⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdonpicocontainer.java

📁 一个非常好的FRAMWRK!是一个外国组织做的!不!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        /**
         * {@inheritDoc}
         * The returned ComponentAdapter will be instantiated by the {@link ComponentAdapterFactory}
         * passed to the container's constructor.
         */
        public ComponentAdapter registerComponentImplementation(Class componentImplementation) throws PicoRegistrationException {
            return registerComponentImplementation(componentImplementation, componentImplementation);
        }

        /**
         * {@inheritDoc}
         * The returned ComponentAdapter will be instantiated by the {@link ComponentAdapterFactory}
         * passed to the container's constructor.
         */
        public ComponentAdapter registerComponentImplementation(Object componentKey, Class componentImplementation) throws PicoRegistrationException {
            return registerComponentImplementation(componentKey, componentImplementation, (Parameter[]) null);
        }

        /**
         * {@inheritDoc}
         * The returned ComponentAdapter will be instantiated by the {@link ComponentAdapterFactory}
         * passed to the container's constructor.
         */
        public ComponentAdapter registerComponentImplementation(Object componentKey, Class componentImplementation, Parameter[] parameters) throws PicoRegistrationException {
            ComponentAdapter componentAdapter = componentAdapterFactory.createComponentAdapter(componentKey, componentImplementation, parameters);
            registerComponent(componentAdapter);
            return componentAdapter;
        }

        /**
         * Same as {@link #registerComponentImplementation(java.lang.Object, java.lang.Class, org.picocontainer.Parameter[])}
         * but with parameters as a {@link List}. Makes it possible to use with Groovy arrays (which are actually Lists).
         */
        public ComponentAdapter registerComponentImplementation(Object componentKey, Class componentImplementation, List parameters) throws PicoRegistrationException {
            Parameter[] parametersAsArray = (Parameter[]) parameters.toArray(new Parameter[parameters.size()]);
            return registerComponentImplementation(componentKey, componentImplementation, parametersAsArray);
        }

        private void addOrderedComponentAdapter(ComponentAdapter componentAdapter) {
            if (!orderedComponentAdapters.contains(componentAdapter)) {
                orderedComponentAdapters.add(componentAdapter);
            }
        }

        public List getComponentInstances() throws PicoException {
            return getComponentInstancesOfType(Object.class);
        }

        public List getComponentInstancesOfType(Class componentType) throws PicoException {
            if(componentType == null) {
                return Collections.EMPTY_LIST;
            }

            Map adapterToInstanceMap = new HashMap();
            for (Iterator iterator = componentAdapters.iterator(); iterator.hasNext();) {
                ComponentAdapter componentAdapter = (ComponentAdapter) iterator.next();
                if (componentType.isAssignableFrom(componentAdapter.getComponentImplementation())) {
                    Object componentInstance = getInstance(componentAdapter);
                    adapterToInstanceMap.put(componentAdapter, componentInstance);

                    // This is to ensure all are added. (Indirect dependencies will be added
                    // from InstantiatingComponentAdapter).
                    addOrderedComponentAdapter(componentAdapter);
                }
            }
            List result = new ArrayList();
            for (Iterator iterator = orderedComponentAdapters.iterator(); iterator.hasNext();) {
                Object componentAdapter = iterator.next();
                final Object componentInstance = adapterToInstanceMap.get(componentAdapter);
                if (componentInstance != null) {
                    // may be null in the case of the "implicit" adapter
                    // representing "this".
                    result.add(componentInstance);
                }
            }
            return result;
        }

        public Object getComponentInstance(Object componentKey) throws PicoException {
            ComponentAdapter componentAdapter = getComponentAdapter(componentKey);
            if (componentAdapter != null) {
                return getInstance(componentAdapter);
            } else {
                return null;
            }
        }

        public Object getComponentInstanceOfType(Class componentType) {
            final ComponentAdapter componentAdapter = getComponentAdapterOfType(componentType);
            return componentAdapter == null ? null : getInstance(componentAdapter);
        }

        /**
         * modify this method of old DefaultPicocontainer
         * @param componentAdapter
         * @return
         */
        public Object getInstance(ComponentAdapter componentAdapter) {     
            Object componentKey = componentAdapter.getComponentKey();
            Object instance = componentKeyToInstanceCache.get(componentKey);
            if (instance == null) {
                if (componentAdapter != null) {
                    instance = getTrueInstance(componentAdapter);
                    if (instance != null){
                        componentKeyToInstanceCache.put(componentKey, instance);
                    }
                }
            }
            return instance;            
        
        }
        
        private Object getTrueInstance(ComponentAdapter componentAdapter) {
      
            // check wether this is our adapter
            // we need to check this to ensure up-down dependencies cannot be followed
            final boolean isLocal = componentAdapters.contains(componentAdapter);

            if (isLocal) {
                Object instance = componentAdapter.getComponentInstance(this);

                addOrderedComponentAdapter(componentAdapter);

                return instance;
            } else if (parent != null) {
                return parent.getComponentInstance(componentAdapter.getComponentKey());
            }

            // TODO: decide .. exception or null?
            // exceptrion: mx: +1, joehni +1
            return null;
        }
        


        public PicoContainer getParent() {
            return parent;
        }

        public ComponentAdapter unregisterComponentByInstance(Object componentInstance) {
            Collection componentAdapters = getComponentAdapters();
            for (Iterator iterator = componentAdapters.iterator(); iterator.hasNext();) {
                ComponentAdapter componentAdapter = (ComponentAdapter) iterator.next();
                if (getInstance(componentAdapter).equals(componentInstance)) {
                    return unregisterComponent(componentAdapter.getComponentKey());
                }
            }
            return null;
        }

        /**
         * @deprecated since 1.1 - Use new VerifyingVisitor().traverse(this)
        */
        public void verify() throws PicoVerificationException {
            new VerifyingVisitor().traverse(this);
        }

        /**
         * Start the components of this PicoContainer and all its logical child containers.
         * Any component implementing the lifecycle interface {@link org.picocontainer.Startable} will be started.
         * @see #makeChildContainer()
         * @see #addChildContainer(PicoContainer)
         * @see #removeChildContainer(PicoContainer)
         */
        public void start() {
            if (disposed) throw new IllegalStateException("Already disposed");
            if (started) throw new IllegalStateException("Already started");
            LifecycleVisitor.start(this);
            started = true;
        }

        /**
         * Stop the components of this PicoContainer and all its logical child containers.
         * Any component implementing the lifecycle interface {@link org.picocontainer.Startable} will be stopped.
         * @see #makeChildContainer()
         * @see #addChildContainer(PicoContainer)
         * @see #removeChildContainer(PicoContainer)
         */
        public void stop() {
            if (disposed) throw new IllegalStateException("Already disposed");
            if (!started) throw new IllegalStateException("Not started");
            LifecycleVisitor.stop(this);
            started = false;
        }

        /**
         * Dispose the components of this PicoContainer and all its logical child containers.
         * Any component implementing the lifecycle interface {@link org.picocontainer.Disposable} will be disposed.
         * @see #makeChildContainer()
         * @see #addChildContainer(PicoContainer)
         * @see #removeChildContainer(PicoContainer)
         */
        public void dispose() {
            if (disposed) throw new IllegalStateException("Already disposed");
            LifecycleVisitor.dispose(this);
            disposed = true;
        }

        public MutablePicoContainer makeChildContainer() {
            DefaultPicoContainer pc = new DefaultPicoContainer(componentAdapterFactory, this);
            addChildContainer(pc);
            return pc;
        }

        public boolean addChildContainer(PicoContainer child) {
            return children.add(child);
        }

        public boolean removeChildContainer(PicoContainer child) {
            final boolean result = children.remove(child);
            return result;
        }

        public void accept(PicoVisitor visitor) {
            visitor.visitContainer(this);
            final List componentAdapters = new ArrayList(getComponentAdapters());
            for (Iterator iterator = componentAdapters.iterator(); iterator.hasNext();) {
                ComponentAdapter componentAdapter = (ComponentAdapter) iterator.next();
                componentAdapter.accept(visitor);
            }
            final List allChildren = new ArrayList(children);
            for (Iterator iterator = allChildren.iterator(); iterator.hasNext();) {
                PicoContainer child = (PicoContainer) iterator.next();
                child.accept(visitor);
            }
        }
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -