From: <to...@us...> - 2007-01-29 23:33:41
|
Revision: 10 http://techne-dev.svn.sourceforge.net/techne-dev/?rev=10&view=rev Author: tonit Date: 2007-01-29 15:33:41 -0800 (Mon, 29 Jan 2007) Log Message: ----------- Added Paths: ----------- sandbox/tonit/techne.farrt/src/techne/farrt/PropertyMetadata.java sandbox/tonit/techne.farrt/src/techne/farrt/ReferenceMetadata.java sandbox/tonit/techne.farrt/src/techne/farrt/ServiceMetadata.java sandbox/tonit/techne.farrt/src/techne/farrt/XmlHandler.java Added: sandbox/tonit/techne.farrt/src/techne/farrt/PropertyMetadata.java =================================================================== --- sandbox/tonit/techne.farrt/src/techne/farrt/PropertyMetadata.java (rev 0) +++ sandbox/tonit/techne.farrt/src/techne/farrt/PropertyMetadata.java 2007-01-29 23:33:41 UTC (rev 10) @@ -0,0 +1,149 @@ +/* + * 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 techne.farrt; + +import org.osgi.service.component.ComponentException; + +/** + * A property descriptor that contains the information for properties + * defined in the descriptor + * + */ +public class PropertyMetadata { + + // Name of the property (required) + private String m_name; + + // Type of the property (optional) + private String m_type = "String"; + + // Value of the type (optional) + private Object m_value; + + // Flag that indicates if this PropertyMetadata has been validated and thus has become immutable + private boolean m_validated = false; + + /** + * Set the name + * + * @param name + */ + public void setName(String name) { + if (m_validated == true) { + return; + } + + m_name = name; + } + + + /** + * Set the type + * + * @param type + */ + public void setType(String type) { + if (m_validated == true) { + return; + } + m_type = type; + } + + /** + * Set the value + * + * @param value + */ + public void setValue(String value) { + if (m_validated == true) { + return; + } + + // 112.4.5 Parsing of the value is done by the valueOf(String) method (P. 291) + // Should the type accept lowercase too? + if(m_type.equals("String")) { + m_value = String.valueOf(value); + } + else if(m_type.equals("Long")) { + m_value = Long.valueOf(value); + } + else if(m_type.equals("Double")) { + m_value = Double.valueOf(value); + } + else if(m_type.equals("Float")) { + m_value = Float.valueOf(value); + } + else if(m_type.equals("Integer")) { + m_value = Integer.valueOf(value); + } + else if(m_type.equals("Byte")) { + m_value = Byte.valueOf(value); + } + else if(m_type.equals("Char")) { + //TODO: verify if this is adequate for Characters + m_value = Byte.valueOf(value); + } + else if(m_type.equals("Boolean")) { + m_value = Boolean.valueOf(value); + } + else if(m_type.equals("Short")) { + m_value = Short.valueOf(value); + } + else { + throw new IllegalArgumentException("Undefined property type '"+m_type+"'"); + } + } + + /** + * Get the name of the property + * + * @return the name of the property + */ + public String getName() { + return m_name; + } + + /** + * Get the type of the property + * + * @return the type of the property + */ + public String getType() { + return m_type; + } + + /** + * Get the value of the property + * + * @return the value of the property as an Object + */ + public Object getValue() { + return m_value; + } + + /** + * Method used to verify if the semantics of this metadata are correct + */ + public void validate(){ + if(m_name == null) + { + throw new ComponentException("Property name attribute is mandatory"); + } + } +} Added: sandbox/tonit/techne.farrt/src/techne/farrt/ReferenceMetadata.java =================================================================== --- sandbox/tonit/techne.farrt/src/techne/farrt/ReferenceMetadata.java (rev 0) +++ sandbox/tonit/techne.farrt/src/techne/farrt/ReferenceMetadata.java 2007-01-29 23:33:41 UTC (rev 10) @@ -0,0 +1,287 @@ +/* + * 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 techne.farrt; + +import org.osgi.service.component.ComponentException; + +/** + * Information associated to a dependency + * + */ +public class ReferenceMetadata { + // Name for the reference (required) + private String m_name = null; + + // Interface name (required) + private String m_interface = null; + + // Cardinality (optional, default="1..1") + private String m_cardinality = "1..1"; + + // Target (optional) + private String m_target; + + // Name of the bind method (optional) + private String m_bind = null; + + // Name of the unbind method (optional) + private String m_unbind = null; + + // Policy attribute (optional, default = static) + private String m_policy = "static"; + + // Flag that is set once the component is verified (its properties cannot be changed) + private boolean m_validated = false; + + // Flags that store the values passed as strings + private boolean m_isStatic = true; + private boolean m_isOptional = false; + private boolean m_isMultiple = false; + + /////////////////////////////////////////////// setters /////////////////////////////////// + + /** + * Setter for the name attribute + * + * @param name + */ + public void setName(String name) { + if(m_validated) { + return; + } + + m_name = name; + } + + /** + * Setter for the interfaceName attribute + * + * @param interfaceName + */ + public void setInterface(String interfaceName) { + if(m_validated) { + return; + } + + m_interface = interfaceName; + + } + + /** + * Setter for the cardinality attribute + * + * @param cardinality + */ + public void setCardinality(String cardinality) { + if(m_validated) { + return; + } + + m_cardinality = cardinality; + + if(!m_cardinality.equals("0..1") && !m_cardinality.equals("0..n") && !m_cardinality.equals("1..1") && !m_cardinality.equals("1..n")) { + throw new IllegalArgumentException ("Cardinality should take one of the following values: 0..1, 0..n, 1..1, 1..n"); + } + if(m_cardinality.equals("0..1") || m_cardinality.equals("0..n")) { + m_isOptional = true; + } + if(m_cardinality.equals("0..n") || m_cardinality.equals("1..n")) { + m_isMultiple = true; + } + } + + /** + * Setter for the policy attribute + * + * @param policy + */ + public void setPolicy(String policy) { + if(m_validated) { + return; + } + + if(!m_policy.equals("static") && !m_policy.equals("dynamic")) { + throw new IllegalArgumentException ("Policy must be either 'static' or 'dynamic'"); + } + if(policy.equals("static") == false) { + m_isStatic = false; + } + + m_policy = policy; + } + + /** + * Setter for the target attribute (filter) + * + * @param target + */ + public void setTarget(String target) { + if(m_validated) { + return; + } + + //TODO: check if we really need to extend the filter to limit seaches to a particular interface + String classnamefilter = "(objectClass="+m_interface+")"; + if(target != null) { + m_target = "(&"+classnamefilter+target+")"; + } + else { + m_target = classnamefilter; + } + } + + /** + * Setter for the bind method attribute + * + * @param bind + */ + public void setBind(String bind) { + if(m_validated) { + return; + } + + m_bind = bind; + } + + /** + * Setter for the unbind method attribute + * + * @param bind + */ + public void setUnbind(String unbind) { + if(m_validated) { + return; + } + + m_unbind = unbind; + } + + + /////////////////////////////////////////////// getters /////////////////////////////////// + + /** + * Returns the name of the reference + * + * @return A string containing the reference's name + **/ + public String getName() { + return m_name; + } + + /** + * Returns the fully qualified name of the class that is used by the component to access the service + * + * @return A string containing a fully qualified name + **/ + public String getInterface() { + return m_interface; + } + + /** + * Get the cardinality as a string + * + * @return A string with the cardinality + **/ + public String getCardinality() { + return m_cardinality; + } + + /** + * Get the policy as a string + * + * @return A string with the policy + **/ + public String getPolicy() { + return m_policy; + } + + /** + * Returns the filter expression that further constrains the set of target services + * + * @return A string with a filter + **/ + public String getTarget() { + return m_target; + } + + /** + * Get the name of a method in the component implementation class that is used to notify that + * a service is bound to the component configuration + * + * @return a String with the name of the bind method + **/ + public String getBind() { + return m_bind; + } + + /** + * Get the name of a method in the component implementation class that is used to notify that + * a service is unbound from the component configuration + * + * @return a String with the name of the unbind method + **/ + public String getUnbind() { + return m_unbind; + } + + // Getters for boolean values that determine both policy and cardinality + + /** + * Test if dependency's binding policy is static + * + * @return true if static + **/ + public boolean isStatic() { + return m_isStatic; + } + + /** + * Test if dependency is optional (0..1 or 0..n) + * + * @return true if the dependency is optional + **/ + public boolean isOptional() { + return m_isOptional; + } + + /** + * Test if dependency is multiple (0..n or 1..n) + * + * @return true if the dependency is multiple + **/ + public boolean isMultiple() { + return m_isMultiple; + } + + /** + * Method used to verify if the semantics of this metadata are correct + * + */ + void validate() { + if (m_name == null) { + throw new ComponentException("the name for the reference must be set"); + } + + if (m_interface == null) { + throw new ComponentException("the interface for the reference must be set"); + } + } + + +} \ No newline at end of file Added: sandbox/tonit/techne.farrt/src/techne/farrt/ServiceMetadata.java =================================================================== --- sandbox/tonit/techne.farrt/src/techne/farrt/ServiceMetadata.java (rev 0) +++ sandbox/tonit/techne.farrt/src/techne/farrt/ServiceMetadata.java 2007-01-29 23:33:41 UTC (rev 10) @@ -0,0 +1,103 @@ +/* + * 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 techne.farrt; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.osgi.service.component.ComponentException; + +/** + * This class contains the metadata associated to a service that is provided + * by a component + * + */ +public class ServiceMetadata { + + // 112.4.6 Flag that indicates if the service is a ServiceFactory + private boolean m_serviceFactory = false; + + // List of provided interfaces + private List m_provides = new ArrayList(); + + // Flag that indicates if this metadata has been validated and has become immutable + private boolean m_validated = false; + + /** + * Setter for the servicefactory attribute of the service element + * + * @param serviceFactory + */ + public void setServiceFactory(boolean serviceFactory) { + if(m_validated) { + return; + } + + m_serviceFactory = serviceFactory; + } + + /** + * Add a provided interface to this service + * + * @param provide a String containing the name of the provided interface + */ + public void addProvide(String provide) { + if(m_validated) { + return; + } + + m_provides.add(provide); + } + + /** + * Return the flag that defines if it is a service factory or not + * + * @return a boolean flag + */ + public boolean isServiceFactory() { + return m_serviceFactory; + } + + /** + * Returns the implemented interfaces + * + * @return the implemented interfaces as a string array + */ + public String [] getProvides() { + String provides[] = new String[m_provides.size()]; + Iterator it = m_provides.iterator(); + int count = 0; + while (it.hasNext()) + { + provides[count++] = it.next().toString(); + } + return provides; + } + + /** + * Verify if the semantics of this metadata are correct + * + */ + void validate() { + if(m_provides.size() == 0) { + throw new ComponentException("At least one provided interface must be given"); + } + } +} Added: sandbox/tonit/techne.farrt/src/techne/farrt/XmlHandler.java =================================================================== --- sandbox/tonit/techne.farrt/src/techne/farrt/XmlHandler.java (rev 0) +++ sandbox/tonit/techne.farrt/src/techne/farrt/XmlHandler.java 2007-01-29 23:33:41 UTC (rev 10) @@ -0,0 +1,211 @@ +/* + * 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 techne.farrt; + +import java.util.Properties; +import java.util.ArrayList; +import java.util.List; + +import techne.farrt.parser.KXml2SAXHandler; +import techne.farrt.parser.ParseException; + +/** + * + * + */ +public class XmlHandler implements KXml2SAXHandler { + + // A reference to the current component + private ComponentMetadata m_currentComponent = null; + + // The current service + private ServiceMetadata m_currentService = null; + + // A list of component descriptors contained in the file + private List m_components = new ArrayList(); + + /** + * Method called when a tag opens + * + * @param uri + * @param localName + * @param qName + * @param attrib + * @exception ParseException + **/ + public void startElement(String uri,String localName,String qName,Properties attrib) throws ParseException + { + try { + + // 112.4.3 Component Element + if (qName.equals("component")) { + + // Create a new ComponentMetadata + m_currentComponent = new ComponentMetadata(); + + // name attribute is mandatory + m_currentComponent.setName(attrib.getProperty("name")); + + // enabled attribute is optional + if(attrib.getProperty("enabled") != null) { + m_currentComponent.setEnabled(attrib.getProperty("enabled").equals("true")); + } + + // immediate attribute is optional + if(attrib.getProperty("immediate") != null) { + m_currentComponent.setImmediate(attrib.getProperty("immediate").equals("true")); + } + + // factory attribute is optional + if(attrib.getProperty("factory") != null) { + m_currentComponent.setFactoryIdentifier(attrib.getProperty("factory")); + } + + // Add this component to the list + m_components.add(m_currentComponent); + } + + // 112.4.4 Implementation + else if (qName.equals("implementation")) + { + // Set the implementation class name (mandatory) + m_currentComponent.setImplementationClassName(attrib.getProperty("class")); + } + // 112.4.5 Properties and Property Elements + else if (qName.equals("property")) { + // 112.4.5: If the value attribute is specified, the body of the element is ignored. + if( attrib.getProperty("value") != null) { + PropertyMetadata prop = new PropertyMetadata(); + + // name attribute is mandatory + prop.setName(attrib.getProperty("name")); + + // type attribute is optional + if(attrib.getProperty("type") != null) { + prop.setType(attrib.getProperty("type")); + } + + // value attribute is optional + if(attrib.getProperty("value") != null) { + prop.setValue(attrib.getProperty("value")); + } + m_currentComponent.addProperty(prop); + } + else { + // TODO: treat the case where property value is not specified (p. 291) + } + // TODO: treat the case where a properties file name is provided (p. 292) + } + else if(qName.equals("properties")) { + // TODO: implement the properties tag + } + // 112.4.6 Service Element + else if (qName.equals("service")) { + + m_currentService = new ServiceMetadata(); + + // servicefactory attribute is optional + if(attrib.getProperty("servicefactory") != null) { + m_currentService.setServiceFactory(attrib.getProperty("servicefactory").equals("true")); + } + + m_currentComponent.setService(m_currentService); + } + else if (qName.equals("provide")) { + m_currentService.addProvide(attrib.getProperty("interface")); + } + + // 112.4.7 Reference element + else if (qName.equals("reference")) { + ReferenceMetadata ref=new ReferenceMetadata (); + ref.setName(attrib.getProperty("name")); + ref.setInterface(attrib.getProperty("interface")); + + // Cardinality + if(attrib.getProperty("cardinality")!= null) { + ref.setCardinality(attrib.getProperty("cardinality")); + } + + if(attrib.getProperty("policy") != null) { + ref.setPolicy(attrib.getProperty("policy")); + } + + //if + ref.setTarget(attrib.getProperty("target")); + ref.setBind(attrib.getProperty("bind")); + ref.setUnbind(attrib.getProperty("unbind")); + + m_currentComponent.addDependency(ref); + } + } + catch(Exception ex) { + ex.printStackTrace(); + throw new ParseException("Exception during parsing",ex); + } + } + + /** + * Method called when a tag closes + * + * @param uri + * @param localName + * @param qName + * @exception ParseException + */ + public void endElement(java.lang.String uri,java.lang.String localName,java.lang.String qName) throws ParseException + { + if (qName.equals("component")) + { + // When the closing tag for a component is found, the component is validated to check if + // the implementation class has been set + m_currentComponent.validate(); + } + } + + /** + * Called to retrieve the service descriptors + * + * @return A list of service descriptors + */ + List getComponentMetadataList() + { + return m_components; + } + + public void characters(char[] ch, int offset, int length) throws Exception { + // Not used + + } + + public void processingInstruction(String target, String data) throws Exception { + // Not used + + } + + public void setLineNumber(int lineNumber) { + // Not used + + } + + public void setColumnNumber(int columnNumber) { + // Not used + + } +} + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |