ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1463977 - in /ace/trunk: org.apache.ace.identification.api/ org.apache.ace.identification.ifconfig/ org.apache.ace.identification.property/ org.apache.ace.identification/ org.apache.ace.identification/.settings/ org.apache.ace.identificati...
Date Wed, 03 Apr 2013 12:50:06 GMT
Author: marrs
Date: Wed Apr  3 12:50:05 2013
New Revision: 1463977

URL: http://svn.apache.org/r1463977
Log:
ACE-333 Merged the o.a.a.identification projects into one.

Added:
    ace/trunk/org.apache.ace.identification/
    ace/trunk/org.apache.ace.identification/.classpath
    ace/trunk/org.apache.ace.identification/.project
    ace/trunk/org.apache.ace.identification/.settings/
    ace/trunk/org.apache.ace.identification/.settings/org.eclipse.jdt.core.prefs
    ace/trunk/org.apache.ace.identification/api.bnd
    ace/trunk/org.apache.ace.identification/bnd.bnd
    ace/trunk/org.apache.ace.identification/build.xml
    ace/trunk/org.apache.ace.identification/ifconfig.bnd
    ace/trunk/org.apache.ace.identification/property.bnd
    ace/trunk/org.apache.ace.identification/src/
    ace/trunk/org.apache.ace.identification/src/org/
    ace/trunk/org.apache.ace.identification/src/org/apache/
    ace/trunk/org.apache.ace.identification/src/org/apache/ace/
    ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/
    ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/Identification.java
    ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/ifconfig/
    ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/ifconfig/Activator.java
    ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/ifconfig/IfconfigIdentification.java
    ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/packageinfo
    ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/property/
    ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/property/Activator.java
    ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/property/PropertyBasedIdentification.java
    ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/property/constants/
    ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/property/constants/IdentificationConstants.java
    ace/trunk/org.apache.ace.identification/test/
    ace/trunk/org.apache.ace.identification/test/org/
    ace/trunk/org.apache.ace.identification/test/org/apache/
    ace/trunk/org.apache.ace.identification/test/org/apache/ace/
    ace/trunk/org.apache.ace.identification/test/org/apache/ace/identification/
    ace/trunk/org.apache.ace.identification/test/org/apache/ace/identification/ifconfig/
    ace/trunk/org.apache.ace.identification/test/org/apache/ace/identification/ifconfig/IfconfigIdentificationTest.java
    ace/trunk/org.apache.ace.identification/test/org/apache/ace/identification/property/
    ace/trunk/org.apache.ace.identification/test/org/apache/ace/identification/property/PropertyBasedIdentificationTest.java
    ace/trunk/org.apache.ace.identification/test/org/apache/ace/identification/property/SimpleIdentificationTest.java
Removed:
    ace/trunk/org.apache.ace.identification.api/
    ace/trunk/org.apache.ace.identification.ifconfig/
    ace/trunk/org.apache.ace.identification.property/

Added: ace/trunk/org.apache.ace.identification/.classpath
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.identification/.classpath?rev=1463977&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.identification/.classpath (added)
+++ ace/trunk/org.apache.ace.identification/.classpath Wed Apr  3 12:50:05 2013
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="src" output="bin_test" path="test"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+	<classpathentry kind="con" path="aQute.bnd.classpath.container"/>
+	<classpathentry kind="con" path="org.testng.TESTNG_CONTAINER"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

Added: ace/trunk/org.apache.ace.identification/.project
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.identification/.project?rev=1463977&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.identification/.project (added)
+++ ace/trunk/org.apache.ace.identification/.project Wed Apr  3 12:50:05 2013
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>org.apache.ace.identification</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>bndtools.core.bndbuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>bndtools.core.bndnature</nature>
+	</natures>
+</projectDescription>

Added: ace/trunk/org.apache.ace.identification/.settings/org.eclipse.jdt.core.prefs
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.identification/.settings/org.eclipse.jdt.core.prefs?rev=1463977&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.identification/.settings/org.eclipse.jdt.core.prefs (added)
+++ ace/trunk/org.apache.ace.identification/.settings/org.eclipse.jdt.core.prefs Wed Apr 
3 12:50:05 2013
@@ -0,0 +1,11 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6

Added: ace/trunk/org.apache.ace.identification/api.bnd
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.identification/api.bnd?rev=1463977&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.identification/api.bnd (added)
+++ ace/trunk/org.apache.ace.identification/api.bnd Wed Apr  3 12:50:05 2013
@@ -0,0 +1,2 @@
+Export-Package: org.apache.ace.identification
+Bundle-Version: 1.0.0

Added: ace/trunk/org.apache.ace.identification/bnd.bnd
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.identification/bnd.bnd?rev=1463977&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.identification/bnd.bnd (added)
+++ ace/trunk/org.apache.ace.identification/bnd.bnd Wed Apr  3 12:50:05 2013
@@ -0,0 +1,9 @@
+-buildpath: \
+	osgi.core,\
+	osgi.cmpn,\
+	org.apache.felix.dependencymanager,\
+	org.mockito.mockito-all,\
+	org.apache.ace.identification.api;version=latest,\
+	org.apache.ace.test;version=latest
+	
+-sub: *.bnd
\ No newline at end of file

Added: ace/trunk/org.apache.ace.identification/build.xml
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.identification/build.xml?rev=1463977&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.identification/build.xml (added)
+++ ace/trunk/org.apache.ace.identification/build.xml Wed Apr  3 12:50:05 2013
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project name="project" default="build">
+
+	<!-- -->
+
+	<import file="../cnf/build.xml" />
+</project>

Added: ace/trunk/org.apache.ace.identification/ifconfig.bnd
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.identification/ifconfig.bnd?rev=1463977&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.identification/ifconfig.bnd (added)
+++ ace/trunk/org.apache.ace.identification/ifconfig.bnd Wed Apr  3 12:50:05 2013
@@ -0,0 +1,3 @@
+Private-Package: org.apache.ace.identification.ifconfig
+Bundle-Activator: org.apache.ace.identification.ifconfig.Activator
+Bundle-Version: 1.0.0

Added: ace/trunk/org.apache.ace.identification/property.bnd
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.identification/property.bnd?rev=1463977&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.identification/property.bnd (added)
+++ ace/trunk/org.apache.ace.identification/property.bnd Wed Apr  3 12:50:05 2013
@@ -0,0 +1,4 @@
+Private-Package: org.apache.ace.identification.property,\
+	org.apache.ace.identification.property.constants
+Bundle-Activator: org.apache.ace.identification.property.Activator
+Bundle-Version: 1.0.0

Added: ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/Identification.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/Identification.java?rev=1463977&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/Identification.java
(added)
+++ ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/Identification.java
Wed Apr  3 12:50:05 2013
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.identification;
+
+/**
+ * The Identification service can be used to identify the system it is running on. This
+ * identification must be unique and must not change.
+ */
+public interface Identification
+{
+
+    /**
+     * Returns the identification of the system.
+     *
+     * @return the identification, or <code>null</code> if the identification
could not be obtained
+     */
+    public String getID();
+
+}
\ No newline at end of file

Added: ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/ifconfig/Activator.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/ifconfig/Activator.java?rev=1463977&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/ifconfig/Activator.java
(added)
+++ ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/ifconfig/Activator.java
Wed Apr  3 12:50:05 2013
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.identification.ifconfig;
+
+import org.apache.ace.identification.Identification;
+import org.apache.felix.dm.DependencyActivatorBase;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.log.LogService;
+
+public class Activator extends DependencyActivatorBase {
+
+    public synchronized void init(BundleContext context, DependencyManager manager) throws
Exception {
+       manager.add(createComponent()
+            .setInterface(new String[] {Identification.class.getName()}, null)
+            .setImplementation(IfconfigIdentification.class)
+            .add(createServiceDependency().setService(LogService.class).setRequired(false))
+            );
+    }
+
+    public void destroy(BundleContext context, DependencyManager manager) throws Exception
{
+        // do nothing
+    }
+}
\ No newline at end of file

Added: ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/ifconfig/IfconfigIdentification.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/ifconfig/IfconfigIdentification.java?rev=1463977&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/ifconfig/IfconfigIdentification.java
(added)
+++ ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/ifconfig/IfconfigIdentification.java
Wed Apr  3 12:50:05 2013
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.identification.ifconfig;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import org.apache.ace.identification.Identification;
+import org.osgi.service.log.LogService;
+
+/**
+ * Implementation of the <code>Identification</code> interface which will determine
a mac-address based ID which is determined
+ * by running the ifconfig command. The first adapter that has been assigned an ip address
is used.
+ *
+ * The identification has been tested on <code>ifconfig 1.42 (2001-04-13)</code>
which comes with Debian Linux. Similar
+ * versions of ifconfig are likely to work.
+ */
+public class IfconfigIdentification implements Identification {
+
+    private static final String IFCONFIG_COMMAND = "ifconfig";
+    private static final String MAC_IDENTIFIER = "HWaddr ";
+    private static final String IP_IDENTIFIER = "inet addr";
+
+    private volatile LogService m_log; // injected by dependency manager
+
+    private String m_targetID = null;
+
+    public synchronized String getID() {
+        if (m_targetID == null) {
+            BufferedReader reader = null;
+            try {
+                Process process = Runtime.getRuntime().exec(IFCONFIG_COMMAND);
+                InputStream inputStream = process.getInputStream();
+                reader = new BufferedReader(new InputStreamReader(inputStream));
+                m_targetID = parseIfconfigOutput(reader).toLowerCase();
+            }
+            catch (IOException ioe) {
+                m_log.log(LogService.LOG_WARNING, "Unable to determine ifconfig based mac-address
target identification.", ioe);
+                return null;
+            }
+            finally {
+                if (reader != null) {
+                    try {
+                        reader.close();
+                    }
+                    catch (IOException e) {
+                        // not much we can do
+                    }
+                }
+            }
+        }
+        return m_targetID;
+    }
+
+    /**
+     * Parses the mac address of the first active adapter from the output of the ifconfig
command.
+     *
+     * @param ifconfigOutput Reader pointing to the output of the ifconfig command.
+     * @return String containing the mac address or <code>null</code> if no valid
mac address could be determined.
+     * @throws java.io.IOException If the specified reader could not be read correctly.
+     */
+    protected String parseIfconfigOutput(BufferedReader ifconfigOutput) throws IOException
{
+        // Sample output (the part that matters):
+        // eth0      Link encap:Ethernet  HWaddr 00:00:21:CF:76:47
+        //           inet addr:192.168.1.65  Bcast:192.168.1.255  Mask:255.255.255.0
+        String previousLine = "";
+        String line;
+        while ((line = ifconfigOutput.readLine()) != null) {
+            if (line.indexOf(IP_IDENTIFIER) != -1) {
+                if (previousLine.indexOf(MAC_IDENTIFIER) != -1) {
+                    String macAddress = previousLine.substring(previousLine.lastIndexOf(MAC_IDENTIFIER)
+ MAC_IDENTIFIER.length(), previousLine.length());
+                    macAddress = macAddress.trim();
+                    if (isValidMac(macAddress)) {
+                        return macAddress;
+                    }
+                    else {
+                        return null;
+                    }
+                }
+            }
+            previousLine = line;
+        }
+        return null;
+    }
+
+    /**
+     * Verifies whether a string contains a valid mac addres, a valid mac address consists
of
+     * 6 pairs of [A-F,a-f,0-9] separated by ':', e.g. <code>0A:F6:33:19:DE:2A</code>.
+     *
+     * @param mac String containing the possible mac address
+     * @return true If the specified string contains a valid mac address, false otherwise.
+     */
+    protected boolean isValidMac(String mac) {
+        if (mac.length() != 17) {
+            return false;
+        }
+        char[] chars = mac.toCharArray();
+        for (int i = 0; i < chars.length; i++) {
+            char c = chars[i];
+            if (i % 3 == 2) {
+                if (':' != c) {
+                    return false;
+                }
+            }
+            else if (!(('0' <= c) && (c <= '9')) &&
+                     !(('a' <= c) && (c <= 'f')) &&
+                     !(('A' <= c) && (c <= 'F'))) {
+                return false;
+            }
+        }
+        return true;
+    }
+}
\ No newline at end of file

Added: ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/packageinfo
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/packageinfo?rev=1463977&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/packageinfo
(added)
+++ ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/packageinfo
Wed Apr  3 12:50:05 2013
@@ -0,0 +1 @@
+version 1.0
\ No newline at end of file

Added: ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/property/Activator.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/property/Activator.java?rev=1463977&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/property/Activator.java
(added)
+++ ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/property/Activator.java
Wed Apr  3 12:50:05 2013
@@ -0,0 +1,119 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.identification.property;
+
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.ace.identification.Identification;
+import org.apache.ace.identification.property.constants.IdentificationConstants;
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyActivatorBase;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedServiceFactory;
+import org.osgi.service.log.LogService;
+
+public class Activator extends DependencyActivatorBase implements ManagedServiceFactory {
+    private static final String MA_NAME = "ma";
+    private DependencyManager m_manager;
+    private BundleContext m_context;
+    private final Map /*<String, Component>*/ m_instances = new HashMap();
+    private volatile LogService m_log;
+    
+    public synchronized void init(BundleContext context, DependencyManager manager) throws
Exception {
+        m_manager = manager;
+        m_context = context;
+        manager.add(createComponent()
+            .setInterface(new String[] {Identification.class.getName()}, null)
+            .setImplementation(PropertyBasedIdentification.class)
+            .add(createConfigurationDependency()
+                .setPid(IdentificationConstants.IDENTIFICATION_PID))
+            .add(createServiceDependency()
+                .setService(LogService.class)
+                .setRequired(false))
+            );
+        manager.add(createComponent()
+           .setInterface(ManagedServiceFactory.class.getName(), new Properties() {{ put(Constants.SERVICE_PID,
IdentificationConstants.IDENTIFICATION_FACTORY_PID); }})
+           .setImplementation(this)
+           .add(createServiceDependency()
+               .setService(LogService.class)
+               .setRequired(false))
+           );
+    }
+
+    public void destroy(BundleContext context, DependencyManager manager) throws Exception
{
+        // do nothing
+    }
+
+    public String getName() {
+        return "Identification Service Factory";
+    }
+
+    public void updated(String pid, Dictionary dict) throws ConfigurationException {
+        String ma = (String) dict.get(MA_NAME);
+        String id = (String) dict.get(IdentificationConstants.IDENTIFICATION_TARGETID_KEY);
+
+        boolean needToAddComponent = false;
+        Component component;
+        synchronized (m_instances) {
+            component = (Component) m_instances.get(pid);
+            if (component == null) {
+                Properties props = new Properties();
+                if ((ma != null) && (ma.length() > 0)) {
+                    props.put(MA_NAME, ma);
+                }
+                props.put(IdentificationConstants.IDENTIFICATION_TARGETID_KEY, id);
+                component = m_manager.createComponent()
+                    .setInterface(Identification.class.getName(), props)
+                    .setImplementation(new PropertyBasedIdentification(id))
+                    .add(createServiceDependency()
+                        .setService(LogService.class)
+                        .setRequired(false)
+                    );
+                m_instances.put(pid, component);
+                needToAddComponent = true;
+            }
+        }
+        if (needToAddComponent) {
+            m_manager.add(component);
+        }
+        else {
+            Object service = component.getService();
+            if (service instanceof PropertyBasedIdentification) {
+                PropertyBasedIdentification identification = (PropertyBasedIdentification)
service;
+                identification.setID(id);
+            }
+        }
+    }
+
+    public void deleted(String pid) {
+        Component log;
+        synchronized (m_instances) {
+            log = (Component) m_instances.remove(pid);
+        }
+        if (log != null) {
+            m_manager.remove(log);
+        }
+    }
+}
\ No newline at end of file

Added: ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/property/PropertyBasedIdentification.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/property/PropertyBasedIdentification.java?rev=1463977&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/property/PropertyBasedIdentification.java
(added)
+++ ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/property/PropertyBasedIdentification.java
Wed Apr  3 12:50:05 2013
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.identification.property;
+
+import java.util.Dictionary;
+
+import org.apache.ace.identification.Identification;
+import org.apache.ace.identification.property.constants.IdentificationConstants;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedService;
+import org.osgi.service.log.LogService;
+
+/**
+ * Simple implementation of the <code>Identification</code> interface. Because
+ * a target identification should not change during it's lifetime the user of this
+ * implementation should set the ID only once.
+ */
+public class PropertyBasedIdentification implements ManagedService, Identification {
+    private final Object LOCK = new Object();
+    private volatile LogService m_log;
+    private String m_targetID;
+    
+    public PropertyBasedIdentification() {
+    }
+    
+    public PropertyBasedIdentification(String id) {
+        setID(id);
+    }
+
+    public String getID() {
+        synchronized (LOCK) {
+            return m_targetID;
+        }
+    }
+    
+    public void setID(String id) {
+        synchronized (LOCK) {
+            if (m_targetID != null) {
+                m_log.log(LogService.LOG_WARNING, "Target ID is being changed from " + m_targetID
+ " to " + id);
+            }
+            m_targetID = id;
+        }
+    }
+
+    public void updated(Dictionary dictionary) throws ConfigurationException {
+        if (dictionary != null) {
+            String id = (String) dictionary.get(IdentificationConstants.IDENTIFICATION_TARGETID_KEY);
+            if ((id == null) || (id.length() == 0)) {
+                // illegal config
+                throw new ConfigurationException(IdentificationConstants.IDENTIFICATION_TARGETID_KEY,
"Illegal target ID supplied");
+            }
+            // legal config, set configuration
+            setID(id);
+        }
+    }
+}
\ No newline at end of file

Added: ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/property/constants/IdentificationConstants.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/property/constants/IdentificationConstants.java?rev=1463977&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/property/constants/IdentificationConstants.java
(added)
+++ ace/trunk/org.apache.ace.identification/src/org/apache/ace/identification/property/constants/IdentificationConstants.java
Wed Apr  3 12:50:05 2013
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.identification.property.constants;
+
+public interface IdentificationConstants {
+    public static final String IDENTIFICATION_PID = "org.apache.ace.identification.property";
+    public static final String IDENTIFICATION_FACTORY_PID = "org.apache.ace.identification.property.factory";
+    public static final String IDENTIFICATION_TARGETID_KEY = "targetID";
+}
\ No newline at end of file

Added: ace/trunk/org.apache.ace.identification/test/org/apache/ace/identification/ifconfig/IfconfigIdentificationTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.identification/test/org/apache/ace/identification/ifconfig/IfconfigIdentificationTest.java?rev=1463977&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.identification/test/org/apache/ace/identification/ifconfig/IfconfigIdentificationTest.java
(added)
+++ ace/trunk/org.apache.ace.identification/test/org/apache/ace/identification/ifconfig/IfconfigIdentificationTest.java
Wed Apr  3 12:50:05 2013
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.identification.ifconfig;
+
+import static org.apache.ace.test.utils.TestUtils.UNIT;
+
+import org.apache.ace.test.utils.TestUtils;
+import org.osgi.service.log.LogService;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+
+public class IfconfigIdentificationTest {
+
+    private IfconfigIdentification m_identification;
+
+    @BeforeTest(alwaysRun = true)
+    protected void setUp() throws Exception {
+        m_identification = new IfconfigIdentification();
+        TestUtils.configureObject(m_identification, LogService.class);
+    }
+
+    @Test(groups = { UNIT })
+    public void testMacAddressVerifying() throws Exception {
+        assert m_identification.isValidMac("FF:FF:FF:FF:FF:FF");
+        assert m_identification.isValidMac("01:23:45:67:89:01");
+        assert m_identification.isValidMac("0D:C3:45:6A:B9:01");
+        assert !m_identification.isValidMac("");
+        assert !m_identification.isValidMac("FF:FF:FF:FF:FF");
+        assert !m_identification.isValidMac("FF:FF:FF:FF:FF:");
+        assert !m_identification.isValidMac("FF:FF:FF:FF:FF:F");
+        assert !m_identification.isValidMac("A:B:C:D:E:F");
+        assert !m_identification.isValidMac("FF:FF:FF:FF:FF:FG");
+        assert !m_identification.isValidMac("FF:FF:FF:FF:FF:FF:");
+        assert !m_identification.isValidMac("FF-FF-FF-FF-FF-FF");
+        assert !m_identification.isValidMac("thisisnotamacaddr");
+    }
+}

Added: ace/trunk/org.apache.ace.identification/test/org/apache/ace/identification/property/PropertyBasedIdentificationTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.identification/test/org/apache/ace/identification/property/PropertyBasedIdentificationTest.java?rev=1463977&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.identification/test/org/apache/ace/identification/property/PropertyBasedIdentificationTest.java
(added)
+++ ace/trunk/org.apache.ace.identification/test/org/apache/ace/identification/property/PropertyBasedIdentificationTest.java
Wed Apr  3 12:50:05 2013
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.identification.property;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+import static org.apache.ace.test.utils.TestUtils.UNIT;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.hamcrest.core.IsNull.nullValue;
+import static org.mockito.Mockito.mock;
+
+import java.lang.reflect.Field;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.ace.identification.property.constants.IdentificationConstants;
+import org.mockito.Mockito;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.log.LogService;
+import org.testng.annotations.Test;
+
+public class PropertyBasedIdentificationTest {
+    @Test(groups = { UNIT })
+    public void getIdWithoutUpdate() {
+        PropertyBasedIdentification basedIdentification = new PropertyBasedIdentification();
+        Assert.assertNull(basedIdentification.getID());
+    }
+
+    @Test(groups = { UNIT })
+    public void getIdWithUpdate() throws ConfigurationException {
+        PropertyBasedIdentification basedIdentification = new PropertyBasedIdentification();
+        Dictionary dict = new Hashtable();
+        dict.put( IdentificationConstants.IDENTIFICATION_TARGETID_KEY, "myTargetId" );
+        basedIdentification.updated( dict );
+        Assert.assertEquals(basedIdentification.getID(), "myTargetId");
+    }
+
+    @Test(groups = { UNIT })
+    public void getIdOverwrite() throws ConfigurationException {
+        PropertyBasedIdentification basedIdentification = new PropertyBasedIdentification();
+        injectServices( basedIdentification );
+
+        Dictionary dict = new Hashtable();
+        dict.put( IdentificationConstants.IDENTIFICATION_TARGETID_KEY, "oldId" );
+        Dictionary dict2 = new Hashtable();
+        dict2.put( IdentificationConstants.IDENTIFICATION_TARGETID_KEY, "newId" );
+
+        basedIdentification.updated( dict );
+        basedIdentification.updated( dict2 );
+
+        Assert.assertEquals(basedIdentification.getID(), "newId");
+    }
+
+    private void injectServices(Object o) {
+        for (Field field : o.getClass().getDeclaredFields()) {
+            if (field.getType() == LogService.class) {
+                field.setAccessible(true);
+                try {
+                    field.set(o, getLogService());
+                }
+                catch (IllegalAccessException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+
+    private LogService getLogService() {
+        return mock(LogService.class);
+    }
+}

Added: ace/trunk/org.apache.ace.identification/test/org/apache/ace/identification/property/SimpleIdentificationTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.identification/test/org/apache/ace/identification/property/SimpleIdentificationTest.java?rev=1463977&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.identification/test/org/apache/ace/identification/property/SimpleIdentificationTest.java
(added)
+++ ace/trunk/org.apache.ace.identification/test/org/apache/ace/identification/property/SimpleIdentificationTest.java
Wed Apr  3 12:50:05 2013
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.identification.property;
+
+import static org.apache.ace.test.utils.TestUtils.UNIT;
+
+import java.util.Properties;
+
+import org.apache.ace.identification.property.constants.IdentificationConstants;
+import org.apache.ace.test.utils.TestUtils;
+import org.osgi.service.log.LogService;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+
+public class SimpleIdentificationTest {
+    private PropertyBasedIdentification m_identification;
+
+    private static final String TEST_ID = "testTargetID";
+
+    @BeforeTest(alwaysRun = true)
+    protected void setUp() throws Exception {
+        m_identification = new PropertyBasedIdentification();
+        TestUtils.configureObject(m_identification, LogService.class);
+    }
+
+    /**
+     * Test simple identification
+     *
+     * @throws Exception
+     */
+    @SuppressWarnings("serial")
+    @Test(groups = { UNIT })
+    public void testSimpleIdentification() throws Exception {
+        m_identification.updated(
+            new Properties() {
+                {put(IdentificationConstants.IDENTIFICATION_TARGETID_KEY, TEST_ID);}
+            });
+        assert TEST_ID.equals(m_identification.getID()) : "target ID does not match configured
target ID";
+    }
+}



Mime
View raw message