Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2010, Red Hat, Inc., and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.as.weld.deployment.processors;
 
 
 
 
 import static org.jboss.as.naming.NamingMessages.MESSAGES;
Abstract object factory that allows for the creation of service references. Object factories that subclass ServiceReferenceObjectFactory can get access to the value of the service described by the reference.

If the factory state is no org.jboss.msc.service.ServiceController.State.UP then the factory will block. If the state is org.jboss.msc.service.ServiceController.State.START_FAILED or org.jboss.msc.service.ServiceController.State.REMOVED (or the state transactions to one of these states while blocking) an exception is thrown.

Author(s):
Stuart Douglas
 
 
     private volatile ServiceRegistry serviceRegistry;

    
Create a reference to a sub class of ServiceReferenceObjectFactory that injects the value of the given service.
 
     public static Reference createReference(final ServiceName serviceClass<? extends ServiceReferenceObjectFactoryfactory) {
         return ModularReference.create(Context.classnew ServiceNameRefAdr("srof"service), factory);
     }
 
     @Override
     public void injectServiceRegistry(ServiceRegistry registry) {
         this. = registry;
     }
 
     @SuppressWarnings("unchecked")
     @Override
     public final Object getObjectInstance(Object objName nameContext nameCtxHashtable<?, ?> environmentthrows Exception {
         final Reference reference = (Referenceobj;
         final ServiceNameRefAdr nameAdr = (ServiceNameRefAdrreference.get("srof");
         if (nameAdr == null) {
             throw .invalidContextReference("srof");
         }
         final ServiceName serviceName = (ServiceName)nameAdr.getContent();
         final ServiceController<?> controller;
         try {
             controller = .getRequiredService(serviceName);
         } catch (ServiceNotFoundException e) {
             throw .cannotResolveService(serviceName);
         }
 
         ServiceReferenceListener listener = new ServiceReferenceListener();
         controller.addListener(listener);
         synchronized (listener) {
             // if we are interrupted just let the exception propagate for now
             while (!listener.finished) {
                 try {
                     listener.wait();
                 } catch (InterruptedException e) {
                     throw .threadInterrupt(serviceName);
                 }
             }
         }
         switch (listener.getState()) {
             case :
                 return getObjectInstance(listener.getValue(), objnamenameCtxenvironment);
             case :
                 throw .cannotResolveService(serviceNamegetClass().getName(), "START_FAILED");
            case :
                throw .cannotResolveService(serviceNamegetClass().getName(), "START_FAILED");
        }
        // we should never get here, as the listener should not notify unless the state was one of the above
        throw .cannotResolveServiceBug(serviceNamegetClass().getName(), listener.getState().toString());
    }

    
Handles the service reference. The parameters are the same as javax.naming.spi.ObjectFactory.getObjectInstance(java.lang.Object,javax.naming.Name,javax.naming.Context,java.util.Hashtable), but with the addition of the service value as the first parameter.
    public Object getObjectInstance(Object serviceValueObject objName nameContext nameCtx,
                                             Hashtable<?, ?> environmentthrows Exception {
        return serviceValue;
    }

    
listener that notifies when the service changes state
    @SuppressWarnings("unchecked")
    private class ServiceReferenceListener extends AbstractServiceListener {
        private State state;
        private boolean finished = false;
        private Object value;
        @Override
        public synchronized void listenerAdded(ServiceController controller) {
            handleStateChange(controller);
        }
        @Override
        public synchronized void dependencyFailed(ServiceController controller) {
            handleStateChange(controller);
        }
        @Override
        public synchronized void transition(final ServiceController controllerfinal ServiceController.Transition transition) {
            switch (transition) {
                case :
                case :
                case : {
                    handleStateChange(controller);
                    break;
                }
            }
        }
        private void handleStateChange(ServiceController controller) {
             = controller.getState();
            if ( == .) {
                 = controller.getValue();
            }
            if ( == . ||  == . ||  == .) {
                controller.removeListener(this);
                 = true;
                notifyAll();
            }
        }
        public State getState() {
            return ;
        }
        public Object getValue() {
            return ;
        }
        public boolean isFinished() {
            return ;
        }
    }
    private static final class ServiceNameRefAdr extends RefAddr {
        private static final long serialVersionUID = 3677121114687908679L;
        private final ServiceName serviceName;
        private ServiceNameRefAdr(String sServiceName serviceName) {
            super(s);
            this. = serviceName;
        }
        public Object getContent() {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X