1 31 package org.objectweb.proactive.core.component.xml; 32 33 import org.objectweb.fractal.api.factory.InstantiationException; 34 import org.objectweb.fractal.api.type.InterfaceType; 35 import org.objectweb.fractal.api.type.TypeFactory; 36 37 import org.objectweb.proactive.core.component.type.ProActiveTypeFactory; 38 import org.objectweb.proactive.core.xml.handler.AbstractUnmarshallerDecorator; 39 import org.objectweb.proactive.core.xml.handler.BasicUnmarshaller; 40 import org.objectweb.proactive.core.xml.handler.CollectionUnmarshaller; 41 import org.objectweb.proactive.core.xml.handler.UnmarshallerHandler; 42 import org.objectweb.proactive.core.xml.io.Attributes; 43 44 import org.xml.sax.SAXException ; 45 46 import java.util.Vector ; 47 48 49 52 public class ComponentTypeHandler extends AbstractUnmarshallerDecorator { 53 Vector interfaceTypes; 54 String typeName; 55 56 public ComponentTypeHandler() { 57 interfaceTypes = new Vector (); 58 addHandler(ComponentsDescriptorConstants.PROVIDES_TAG, 59 new ProvidesHandler()); 60 addHandler(ComponentsDescriptorConstants.REQUIRES_TAG, 61 new RequiresHandler()); 62 } 63 64 68 public void reset() { 69 interfaceTypes.removeAllElements(); 70 typeName = null; 71 } 72 73 76 protected void notifyEndActiveHandler(String name, 77 UnmarshallerHandler activeHandler) throws SAXException { 78 } 79 80 83 public Object getResultObject() throws SAXException { 84 try { 85 interfaceTypes.trimToSize(); 87 InterfaceType[] itf_types = (InterfaceType[]) interfaceTypes.toArray(new InterfaceType[interfaceTypes.size()]); 88 return new Object [] { 89 typeName, 90 ProActiveTypeFactory.instance().createFcType(itf_types) 91 }; 92 } catch (InstantiationException e) { 93 throw new SAXException ("cannot create component type"); 94 } 95 } 96 97 100 public void startContextElement(String name, Attributes attributes) 101 throws SAXException { 102 String type_name = attributes.getValue(ComponentsDescriptorConstants.COMPONENT_TYPE_NAME_TAG); 103 if (!checkNonEmpty(type_name)) { 104 throw new SAXException ( 105 "the name of the component type needs to be specified"); 106 } 107 typeName = type_name; 108 } 109 110 private class ProvidesHandler extends CollectionUnmarshaller { 113 private ProvidesHandler() { 114 addHandler(ComponentsDescriptorConstants.INTERFACE_TAG, 115 new InterfaceHandler(ComponentsDescriptorConstants.PROVIDES_TAG)); 116 } 117 118 121 protected void notifyEndActiveHandler(String name, 122 UnmarshallerHandler activeHandler) throws SAXException { 123 } 124 125 128 public Object getResultObject() throws SAXException { 129 return null; 130 } 131 } 132 133 private class RequiresHandler extends CollectionUnmarshaller { 136 private RequiresHandler() { 137 addHandler(ComponentsDescriptorConstants.INTERFACE_TAG, 138 new InterfaceHandler(ComponentsDescriptorConstants.REQUIRES_TAG)); 139 } 140 141 144 protected void notifyEndActiveHandler(String name, 145 UnmarshallerHandler activeHandler) throws SAXException { 146 } 147 } 148 149 protected class InterfaceHandler extends BasicUnmarshaller { 151 boolean isClient; 152 boolean contingency; 153 boolean cardinality; 154 155 private InterfaceHandler(String role) { 156 if (role.equals(ComponentsDescriptorConstants.REQUIRES_TAG)) { 157 isClient = true; 158 } else if (role.equals(ComponentsDescriptorConstants.PROVIDES_TAG)) { 159 isClient = false; 160 } else { 161 throw new RuntimeException ( 162 "none of the following tags was found : " + 163 ComponentsDescriptorConstants.REQUIRES_TAG + ',' + 164 ComponentsDescriptorConstants.PROVIDES_TAG); 165 } 166 } 167 168 public void startContextElement(String name, Attributes attributes) 169 throws org.xml.sax.SAXException { 170 ComponentHandler.logger.debug("#####startContextElement : " + name); 171 String itf_name = attributes.getValue(ComponentsDescriptorConstants.INTERFACE_NAME_TAG); 173 if (!checkNonEmpty(itf_name)) { 174 throw new SAXException ("interface name unspecified"); 175 } 176 String itf_signature = attributes.getValue(ComponentsDescriptorConstants.INTERFACE_SIGNATURE_TAG); 177 if (!checkNonEmpty(itf_signature)) { 178 throw new SAXException ("interface signature unspecified"); 179 } 180 String value = attributes.getValue(ComponentsDescriptorConstants.INTERFACE_CONTINGENCY_TAG); 181 if (!checkNonEmpty(value) || 182 value.equals( 183 ComponentsDescriptorConstants.INTERFACE_CONTINGENCY_OPTIONAL_TAG)) { 184 contingency = TypeFactory.OPTIONAL; 185 } else if (value.equals( 186 ComponentsDescriptorConstants.INTERFACE_CONTINGENCY_MANDATORY_TAG)) { 187 contingency = TypeFactory.MANDATORY; 188 } else { 189 throw new SAXException ("contingency values are : " + 190 ComponentsDescriptorConstants.INTERFACE_CONTINGENCY_MANDATORY_TAG + 191 ", " + 192 ComponentsDescriptorConstants.INTERFACE_CONTINGENCY_OPTIONAL_TAG + 193 ", or nothing (if schema validation is enabled) (default is " + 194 ComponentsDescriptorConstants.INTERFACE_CONTINGENCY_OPTIONAL_TAG + 195 ")"); 196 } 197 198 value = attributes.getValue(ComponentsDescriptorConstants.INTERFACE_CARDINALITY_TAG); 199 if (checkNonEmpty(value)) { 200 if (value.equals( 201 ComponentsDescriptorConstants.INTERFACE_CARDINALITY_SINGLE_TAG)) { 202 cardinality = TypeFactory.SINGLE; 203 } else if (value.equals( 204 ComponentsDescriptorConstants.INTERFACE_CARDINALITY_COLLECTIVE_TAG)) { 205 cardinality = TypeFactory.COLLECTION; 206 } else { 207 throw new SAXException ("cardinality values are : " + 208 ComponentsDescriptorConstants.INTERFACE_CARDINALITY_COLLECTIVE_TAG + 209 ", " + 210 ComponentsDescriptorConstants.INTERFACE_CARDINALITY_SINGLE_TAG + 211 ", or nothing (if schema validation is enabled) (default is " + 212 ComponentsDescriptorConstants.INTERFACE_CARDINALITY_SINGLE_TAG + 213 ") "); 214 } 215 } 216 217 InterfaceType itf_type = null; 218 try { 219 itf_type = ProActiveTypeFactory.instance().createFcItfType(itf_name, 220 itf_signature, isClient, contingency, cardinality); 221 } catch (InstantiationException e) { 222 throw new SAXException (e); 223 } 224 225 if (logger.isDebugEnabled()) { 227 ComponentHandler.logger.debug("new interface type added: " + 228 itf_type.getFcItfName()); 229 } 230 231 interfaceTypes.add(itf_type); 232 } 235 } 236 } 237 | Popular Tags |