Osgi Config ManagedService Dictionary键不区分大小写

时间:2017-08-17 20:31:13

标签: osgi aem apache-felix

我遇到了一个问题。我有一个package rotation; import java.awt.*; import java.awt.image.*; import java.swing.*; import java.swing.event.*; public class Rotation { private double theta; public Rotation() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(new Rectangle(650, 650)); frame.setResizable(false); Container contentPane = frame.getContentPane(); contentPane.setLayout(null); BufferedImage image = new BufferedImage(400, 400, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = (Graphics2D) image.createGraphics(); g2.setColor(Color.red); g2.fillRect(0, 0, image.getWidth(), image.getHeight()); g2.dispose(); JPanel panel = new JPanel() { @Override public Dimension getPreferredSize() { BufferedImage img = rotate(image, theta); return new Dimension(img.getWidth(), img.getHeight()); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); int middleX = getWidth() / 2; int middleY = getHeight() / 2; BufferedImage img = rotate(image, theta); g.drawImage(img, middleX - img.getWidth() / 2, middleY - img.getHeight() / 2, null); } }; contentPane.add(panel); panel.setLocation(0, 0); panel.setSize(panel.getPreferredSize()); JSlider slider = new JSlider(0, 360, 0); slider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { theta = slider.getValue(); panel.revalidate(); panel.repaint(); } }); contentPane.add(slider); slider.setLocation(0, 600); slider.setSize(slider.getPreferredSize()); contentPane.revalidate(); contentPane.repaint(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public BufferedImage rotate(BufferedImage image, double angle) { angle = Math.toRadians(angle); double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle)); int w = image.getWidth(), h = image.getHeight(); int neww = (int)Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin); GraphicsConfiguration gc = getDefaultConfiguration(); BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT); Graphics2D g2 = result.createGraphics(); g2.translate((neww - w) / 2, (newh - h) / 2); g2.rotate(angle, w / 2, h / 2); g2.drawRenderedImage(image, null); g2.dispose(); Graphics2D g = (Graphics2D) result.getGraphics(); g.setColor(Color.black); g.drawRect(0, 0, neww - 1, newh - 1); return result; } public GraphicsConfiguration getDefaultConfiguration() { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); return gd.getDefaultConfiguration(); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { new Rotation(); } }); } } impl客户端用于osgi配置。配置是键值对的集合。 当传递给org.osgi.service.cm.ManagedService的更新方法(ManagedService.updated)作为ManagedService对象时,这些属性中的键似乎不区分大小写,即java.util.Dictionary即使关键也可以工作在配置中是“你好”。

当我通过遍历其条目将该字典转换为props.get("HellO")时,映射中的键将按预期区分大小写。预计Hashmap中的密钥是否不区分大小写?

这是在AEM 6.2实例上测试的。

这是我的Dictionary impl类。

ManagedService

使用以下代码将public class ConfigService implements ManagedService { public void updated(final Dictionary props) throws ConfigurationException { // props.get("HellO") returns value if (props != null) { String pid = (String) props.get(Constants.SERVICE_PID); // convert to map Map map = map(props); // map.get("HellO") returns null // map.get("Hello") returns value } } private static Map map(Dictionary dict) { Map map = new ConcurrentHashMap(); for (Enumeration keys = dict.keys(); keys.hasMoreElements();) { Object key = keys.nextElement(); map.put(key, dict.get(key)); } return map; } impl注册为服务。

ManagedService

1 个答案:

答案 0 :(得分:3)

这符合Configuration Admin Service规范。

  
    

属性的名称或键必须始终是String对象,而不是     查找时区分大小写,但必须保留原始案例。

  

Apache felix源代码供参考。 https://github.com/apache/felix/blob/trunk/configadmin/src/main/java/org/apache/felix/cm/impl/CaseInsensitiveDictionary.java

  
    

/ **      * CaseInsensitiveDictionary是一个      * java.util.Dictionary符合要求      *由需要该属性的配置管理服务规范输出      *名称保留大小写,但在访问属性时忽略大小写。      * /