Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2008, 2010 VMware Inc. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: VMware Inc. - initial contribution /
 
 
 package org.eclipse.virgo.nano.shim.scope.internal;
 
 import java.util.Map;
 
Creates org.eclipse.virgo.nano.shim.scope.Scope instances for ServiceReferences and for lookups.

Concurrent Semantics
Threadsafe.

 
 public final class StandardScopeFactory implements ScopeFactory {
 
     private static final String PROPERTY_BLUEPRINT_CONTEXT_SERVICE_NAME = "org.eclipse.gemini.blueprint.context.service.name";
 
     private static final String PROPERTY_BEAN_NAME = "org.eclipse.gemini.blueprint.bean.name";
 
     private final EventLogger eventLogger;
 
     /*
      * Share application scope properties between equivalent application scope instances
      */
 
     public StandardScopeFactory(EventLogger eventLogger) {
         this. = eventLogger;
     }

    
 
     public Scope getBundleScope(Bundle bundle) {
         return isBundleScoped(bundle) ? createApplicationScope(bundle) : .;
 
     }

    

Parameters:
bundle
Returns:
 
     private Scope createApplicationScope(Bundle bundle) {
         return getApplicationScope(getScopeName(bundle));
     }

    
 
     public Scope getGlobalScope() {
         return .;
     }

    
 
     public Scope getServiceScope(ServiceReference ref) {
         return ..equals(getScopeIdentifier(ref)) ? getBundleScope(ref.getBundle()) : getGlobalScope();
     }

    
 
     public Scope getApplicationScope(String applicationScopeName) {
         synchronized (this.) {
             AppScope appScope = new AppScope(applicationScopeName);
             ConcurrentHashMap<StringObjectprops = this..get(appScope);
             if (props == null) {
                 props = new ConcurrentHashMap<StringObject>();
                 this..put(appScopeprops);
             }
             appScope.setProperties(props);
 
             return appScope;
         }
     }
 
     private String getScopeIdentifier(ServiceReference ref) {
         String serviceScope = (Stringref.getProperty(.);
        if (serviceScope == null) {
            /*
             * Tolerate the former property name to avoid breaking dm Server 2.0.x users. Post 2.1.0 the former property
             * name need not be supported. Issue a warning message to prompt users to change the name.
             */
            serviceScope = (Stringref.getProperty("com.springsource.service.scope");
            if (serviceScope == null) {
                // Blueprint (application) contexts belong in the global scope.
                if (ref.getProperty() != null) {
                    serviceScope = .;
                }
                else
                if (isBundleScoped(ref.getBundle())) {
                    serviceScope = .;
                } else {
                    serviceScope = .;
                }
            } else {
                    ref.getProperty());
            }
        }
        return serviceScope;
    }
    private static boolean isBundleScoped(Bundle bundle) {
        // TODO: reinstate return OsgiFrameworkUtils.getScopeName(bundle) != null;
        return getScopeName(bundle) != null;
    }
    private static String getScopeName(Bundle bundle) {
        // TODO: use OFUtils instead when in proper location
        return (Stringbundle.getHeaders().get("Module-Scope");
    }

    
    public void destroyApplicationScope(Scope applicationScope) {
        // TODO: reinstate Assert.isTrue(applicationScope instanceof AppScope, "wrong scope type");
        AppScope appScope = (AppScopeapplicationScope;
        synchronized (this.) {
            this..remove(appScope);
        }
    }
    private abstract static class StandardScope implements Scope {
        private volatile ConcurrentHashMap<StringObjectproperties;
        protected StandardScope() {
        }
        final void setProperties(ConcurrentHashMap<StringObjectproperties) {
            // TODO: reinstate Assert.isNull(this.properties, "properties can only be set once");
            this. = properties;
        }

        
        public final Object getProperty(String propertyName) {
            // TODO: reinstate Assert.isTrue(this.properties != null, "properties not set");
            return this..get(propertyName);
        }

        
        public final void setProperty(String propertyNameObject propertyValue) {
            // TODO: reinstate Assert.isTrue(this.properties != null, "properties not set");
            this..put(propertyNamepropertyValue);
        }

        
        public int hashCode() {
            return 0;
        }

        
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            StandardScope other = (StandardScopeobj;
            /*
             * Instances that have not had their properties set compare equal (unless overridden by subclasses).
             * Instances that have had their properties set must use the same properties object reference to compare
             * equal.
             */
            if ( == null || other.properties == null) {
                return true;
            } else if ( != other.properties) {
                return false;
            }
            return true;
        }
    }
    private static class GlobalScope extends StandardScope {
        static GlobalScope INSTANCE = new GlobalScope();
        private GlobalScope() {
            setProperties(new ConcurrentHashMap<StringObject>());
        }

        
        public int hashCode() {
            return 316;
        }
        public boolean equals(Object other) {
            return other == this || other instanceof GlobalScope;
        }
        public String toString() {
            return .;
        }
        public boolean isGlobal() {
            return true;
        }
        public String getScopeName() {
            // The global scope does not have a name.
            return null;
        }
    }
    private static class AppScope extends StandardScope {
        private final String scopeName;
        public AppScope(String scopeName) {
            this. = scopeName;
        }

        
        public int hashCode() {
            final int prime = 31;
            int result = super.hashCode();
            result = prime * result + (( == null) ? 0 : .hashCode());
            return result;
        }

        
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!super.equals(obj)) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            AppScope other = (AppScopeobj;
            if ( == null) {
                if (other.scopeName != null) {
                    return false;
                }
            } else if (!.equals(other.scopeName)) {
                return false;
            }
            return true;
        }

        
        public String toString() {
            return . + ":" + this.;
        }

        
        public boolean isGlobal() {
            return false;
        }
        public String getScopeName() {
            return this.;
        }
    }
New to GrepCode? Check out our FAQ X