geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r778414 - in /geronimo/sandbox/blueprint: blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/ blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/
Date Mon, 25 May 2009 12:53:43 GMT
Author: gnodet
Date: Mon May 25 12:53:43 2009
New Revision: 778414

URL: http://svn.apache.org/viewvc?rev=778414&view=rev
Log:
Fix id generation to make sure we don't use user defined ids

Modified:
    geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/CmNamespaceHandler.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/Parser.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/RecipeBuilder.java

Modified: geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/CmNamespaceHandler.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/CmNamespaceHandler.java?rev=778414&r1=778413&r2=778414&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/CmNamespaceHandler.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/CmNamespaceHandler.java
Mon May 25 12:53:43 2009
@@ -22,6 +22,7 @@
 import java.util.Collections;
 import java.util.List;
 import java.util.ArrayList;
+import java.util.Set;
 
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
@@ -101,7 +102,7 @@
 
     private static final Logger LOGGER = LoggerFactory.getLogger(CmNamespaceHandler.class);
 
-    private int nameCounter;
+    private int idCounter;
 
     public URL getSchemaLocation(String namespace) {
         return getClass().getResource("blueprint-cm.xsd");
@@ -142,7 +143,7 @@
     private ComponentMetadata parsePropertyPlaceholder(ExtendedParserContext context, Element
element) {
         MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
         metadata.setProcessor(true);
-        metadata.setId(getName(element));
+        metadata.setId(getId(context, element));
         metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
         metadata.setRuntimeClass(CmPropertyPlaceholder.class);
         metadata.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
@@ -199,10 +200,10 @@
     }
 
     private ComponentMetadata parseManagedServiceFactory(ExtendedParserContext context, Element
element) {
-        String id = getName(element);
+        String id = getId(context, element);
 
         MutableBeanMetadata factoryMetadata = context.createMetadata(MutableBeanMetadata.class);
-        generateIdIfNeeded(factoryMetadata);        
+        generateIdIfNeeded(context, factoryMetadata);
         factoryMetadata.setScope(BeanMetadata.SCOPE_SINGLETON);
         factoryMetadata.setRuntimeClass(CmManagedServiceFactory.class);
         factoryMetadata.setInitMethodName("init");
@@ -250,7 +251,7 @@
                 } else if (BLUEPRINT_CM_NAMESPACE.equals(e.getNamespaceURI())) {
                     if (nodeNameEquals(e, MANAGED_COMPONENT_ELEMENT)) {
                         MutableBeanMetadata managedComponent = context.parseElement(MutableBeanMetadata.class,
null, e);
-                        generateIdIfNeeded(managedComponent);
+                        generateIdIfNeeded(context, managedComponent);
                         managedComponent.setScope(BeanMetadata.SCOPE_PROTOTYPE);
                         // destroy-method on managed-component has different signature than
on regular beans
                         // so we'll handle it differently
@@ -288,10 +289,10 @@
         if (!(component instanceof MutableBeanMetadata)) {
             throw new ComponentDefinitionException("Element " + MANAGED_PROPERTIES_ELEMENT
+ " must be used inside a <bp:bean> element");
         }
-        generateIdIfNeeded(((MutableBeanMetadata) component));
+        generateIdIfNeeded(context, ((MutableBeanMetadata) component));
         MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
         metadata.setProcessor(true);
-        metadata.setId(getName(element));
+        metadata.setId(getId(context, element));
         metadata.setRuntimeClass(CmManagedProperties.class);
         String persistentId = element.getAttribute(PERSISTENT_ID_ATTRIBUTE);
         // if persistentId is "" the managed properties element in nested in managed-service-factory
@@ -406,18 +407,26 @@
         return BLUEPRINT_NAMESPACE.equals(ns);
     }
 
-    public String getName(Element element) {
+    public String getId(ExtendedParserContext context, Element element) {
         if (element.hasAttribute(ID_ATTRIBUTE)) {
             return element.getAttribute(ID_ATTRIBUTE);
         } else {
-            return "cm-" + ++nameCounter;
+            return generateId(context);
         }
     }
 
-    public void generateIdIfNeeded(MutableBeanMetadata metadata) {
+    public void generateIdIfNeeded(ExtendedParserContext context, MutableBeanMetadata metadata)
{
         if (metadata.getId() == null) {
-            metadata.setId("cm-" + ++nameCounter);
+            metadata.setId(generateId(context));
         }
     }
 
+    private String generateId(ExtendedParserContext context) {
+        String id;
+        do {
+            id = "#cm-" + ++idCounter;
+        } while (context.getComponentDefinitionRegistry().containsComponentDefinition(id));
+        return id;
+    }
+
 }

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/Parser.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/Parser.java?rev=778414&r1=778413&r2=778414&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/Parser.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/Parser.java
Mon May 25 12:53:43 2009
@@ -195,8 +195,9 @@
     private List<Document> documents;
     private ExtendedComponentDefinitionRegistry registry;
     private NamespaceHandlerRegistry namespaceHandlerRegistry;
-    private String namePrefix = "component-";
-    private int nameCounter;
+    private String idPrefix = "component-";
+    private Set<String> ids = new HashSet<String>();
+    private int idCounter;
     private String defaultTimeout;
     private String defaultAvailability;
     private String defaultLazyInit;
@@ -207,8 +208,8 @@
     public Parser() {
     }
 
-    public Parser(String namePrefix) {
-        this.namePrefix = namePrefix;
+    public Parser(String idPrefix) {
+        this.idPrefix = idPrefix;
     }
 
     public void setValidation(boolean validation) {
@@ -439,7 +440,7 @@
     private ComponentMetadata parseBeanMetadata(Element element, boolean topElement) {
         BeanMetadataImpl metadata = new BeanMetadataImpl();
         if (topElement) {
-            metadata.setId(getName(element));
+            metadata.setId(getId(element));
         }
         if (element.hasAttribute(CLASS_ATTRIBUTE)) {
             metadata.setClassName(element.getAttribute(CLASS_ATTRIBUTE));
@@ -533,7 +534,7 @@
         ServiceMetadataImpl service = new ServiceMetadataImpl();
         boolean hasInterfaceNameAttribute = false;
         if (topElement) {
-            service.setId(getName(element));
+            service.setId(getId(element));
         }
         if (element.hasAttribute(INTERFACE_ATTRIBUTE)) {
             service.setInterfaceNames(Collections.singletonList(element.getAttribute(INTERFACE_ATTRIBUTE)));
@@ -790,7 +791,7 @@
     private ComponentMetadata parseReference(Element element, boolean topElement) {     
 
         ReferenceMetadataImpl reference = new ReferenceMetadataImpl();
         if (topElement) {
-            reference.setId(getName(element));
+            reference.setId(getId(element));
         }
         parseReference(element, reference);
         String timeout = element.hasAttribute(TIMEOUT_ATTRIBUTE) ? element.getAttribute(TIMEOUT_ATTRIBUTE)
: this.defaultTimeout;
@@ -811,7 +812,7 @@
     private ComponentMetadata parseRefCollection(Element element, Class collectionType, boolean
topElement) {
         RefCollectionMetadataImpl references = new RefCollectionMetadataImpl();
         if (topElement) {
-            references.setId(getName(element));
+            references.setId(getId(element));
         }
         references.setCollectionType(collectionType);
 
@@ -1129,12 +1130,17 @@
         return handler;
     }
     
-    public String getName(Element element) {
+    public String getId(Element element) {
+        String id;
         if (element.hasAttribute(ID_ATTRIBUTE)) {
-            return element.getAttribute(ID_ATTRIBUTE);
+            id = element.getAttribute(ID_ATTRIBUTE);
         } else {
-            return namePrefix + ++nameCounter;
+            do {
+                id = idPrefix + ++idCounter;
+            } while (ids.contains(id));
         }
+        ids.add(id);
+        return id;
     }
 
     public static boolean isBlueprintNamespace(String ns) {

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/RecipeBuilder.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/RecipeBuilder.java?rev=778414&r1=778413&r2=778414&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/RecipeBuilder.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/RecipeBuilder.java
Mon May 25 12:53:43 2009
@@ -24,6 +24,8 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Properties;
+import java.util.Set;
+import java.util.HashSet;
 
 import org.apache.geronimo.blueprint.ExtendedBlueprintContainer;
 import org.apache.geronimo.blueprint.ExtendedComponentDefinitionRegistry;
@@ -37,7 +39,6 @@
 import org.apache.geronimo.blueprint.di.ValueRecipe;
 import org.apache.geronimo.blueprint.mutable.MutableMapMetadata;
 import org.apache.geronimo.blueprint.reflect.MetadataUtil;
-import org.osgi.service.blueprint.container.Converter;
 import org.osgi.service.blueprint.reflect.BeanArgument;
 import org.osgi.service.blueprint.reflect.BeanMetadata;
 import org.osgi.service.blueprint.reflect.BeanProperty;
@@ -65,6 +66,7 @@
  */
 public class RecipeBuilder {
 
+    private Set<String> names = new HashSet<String>();
     private int nameCounter;
     private ExtendedBlueprintContainer blueprintContainer;
     private ExtendedComponentDefinitionRegistry registry;
@@ -301,12 +303,13 @@
     }
 
     private String getName(String name) {
-        // TODO: what if name.startWith("recipe-") ?
         if (name == null) {
-            return "recipe-" + ++nameCounter;
-        } else {
-            return name;
+            do {
+                name = "#recipe-" + ++nameCounter;
+            } while (names.contains(name) || registry.containsComponentDefinition(name));
         }
+        names.add(name);
+        return name;
     }
 
 }



Mime
View raw message