WsArtifactsFactory.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.management.wsdm.capabilities;
022 
023 import java.util.HashMap;
024 import java.util.Hashtable;
025 import java.util.Map;
026 
027 import javax.management.MBeanInfo;
028 import javax.management.MBeanServer;
029 import javax.management.ObjectName;
030 
031 import org.apache.muse.core.Environment;
032 import org.apache.muse.core.Resource;
033 import org.apache.qpid.management.Messages;
034 import org.apache.qpid.management.Names;
035 import org.apache.qpid.transport.util.Logger;
036 
037 /**
038  * Manager for all WS-* related artifacts.
039  * Basically it is a factory ehnanced with a _cache mechanism so each created resource 
040  * (WSDL, capability class, descriptor) is created and its reference is returned when requested 
041  * again.
042  
043  @author Andrea Gazzarini
044  */
045 class WsArtifactsFactory 
046 {
047   private final static Logger LOGGER = Logger.get(WsArtifactsFactory.class);
048   
049   private final MBeanServer _mxServer;
050   private final Environment _environment;
051   private Map<ObjectName, WsArtifacts> _cache; 
052 
053   /**
054    * Builds a new factory with the given environment and mbean server.
055    
056    @param environment the builder environment.
057    @param mxServer the management server.
058    */
059   public WsArtifactsFactory(Environment environment, MBeanServer mxServer
060   {
061     this._environment = environment;
062     this._mxServer = mxServer;
063     this._cache = new HashMap<ObjectName, WsArtifacts>();
064   }
065   
066   /**
067    * Returns the WS artifacts corresponding with the given resource.
068    
069    @param resource the WS resource.
070    @param objectName the resource identifier (name).
071    @return the WS artifacts corresponding with the given resource.
072    @throws ArtifactsNotAvailableException when some problem occurs during artifacts generation.
073    */
074   @SuppressWarnings("unchecked")
075   WsArtifacts getArtifactsFor(Resource resource, ObjectName objectNamethrows ArtifactsNotAvailableException
076   {
077     WsArtifacts result = null;
078     try 
079     {
080       Hashtable<String, String> keyProperties = objectName.getKeyPropertyList();
081       keyProperties.remove(Names.NAME_ATTRIBUTE);
082       keyProperties.remove(Names.OBJECT_ID);
083       
084       ObjectName searchKey = ObjectName.getInstance(objectName.getDomain(),keyProperties);
085       
086       LOGGER.debug(
087           Messages.QMAN_200041_INCOMING_OBJECT_NAME_AND_DERIVED_KEY,
088           objectName,
089           searchKey);
090             
091       result = _cache.get(searchKey);
092       if (result == null)
093       {
094         MBeanInfo metadata = _mxServer.getMBeanInfo(objectName);
095         
096         WSDMArtifactsDirector director = new WSDMArtifactsDirector(objectName,metadata);
097         director.setEnvironment(_environment);
098         director.setResource(resource);
099         director.direct();
100         
101         result = new WsArtifacts(
102             director.getCapabilityClass(),
103             director.getResourceMetadataDescriptor(),
104             director.getWsdl());
105         
106         _cache.put(searchKey, result);
107 
108         LOGGER.debug(Messages.QMAN_200040_WS_ARTIFACTS_CACHED,searchKey);
109       }
110       
111       return result;
112     catch(Exception exception)
113     {
114       throw new ArtifactsNotAvailableException(result,exception,objectName);
115     }
116   }
117   
118   /**
119    * Utility method for create concrete instance of the given capability class.
120    
121    @param capabilityClass the capability class.
122    @param objectName the object name that will act as the target for this capability invocations.
123    @return an initialized instance of the given capability class.
124    @throws InstantiationException when the class cannot be instantiated.
125    @throws IllegalAccessException when this method does not have access to 
126    *         the definition of the capability class.
127    */
128   MBeanCapability createCapability(Class<MBeanCapability> capabilityClass, ObjectName objectName
129     throws InstantiationException, IllegalAccessException
130   {
131     MBeanCapability capability = capabilityClass.newInstance();
132     capability.setResourceObjectName(objectName);
133     return capability;
134   }
135 }