summaryrefslogtreecommitdiff
path: root/qpid/java/management/client/src/main/java/org/apache/qpid/management/wsdm/capabilities/WsArtifactsFactory.java
blob: 2a1bf059c341fb2a52677d451345a6f41ff8ec8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.management.wsdm.capabilities;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.apache.muse.core.Environment;
import org.apache.muse.core.Resource;
import org.apache.qpid.management.Messages;
import org.apache.qpid.management.Names;
import org.apache.qpid.transport.util.Logger;

/**
 * Manager for all WS-* related artifacts.
 * Basically it is a factory ehnanced with a _cache mechanism so each created resource 
 * (WSDL, capability class, descriptor) is created and its reference is returned when requested 
 * again.
 * 
 * @author Andrea Gazzarini
 */
class WsArtifactsFactory 
{
	private final static Logger LOGGER = Logger.get(WsArtifactsFactory.class);
	
	private final MBeanServer _mxServer;
	private final Environment _environment;
	private Map<ObjectName, WsArtifacts> _cache; 

	/**
	 * Builds a new factory with the given environment and mbean server.
	 * 
	 * @param environment the builder environment.
	 * @param mxServer the management server.
	 */
	public WsArtifactsFactory(Environment environment, MBeanServer mxServer) 
	{
		this._environment = environment;
		this._mxServer = mxServer;
		this._cache = new HashMap<ObjectName, WsArtifacts>();
	}
	
	/**
	 * Returns the WS artifacts corresponding with the given resource.
	 * 
	 * @param resource the WS resource.
	 * @param objectName the resource identifier (name).
	 * @return the WS artifacts corresponding with the given resource.
	 * @throws ArtifactsNotAvailableException when some problem occurs during artifacts generation.
	 */
	@SuppressWarnings("unchecked")
	WsArtifacts getArtifactsFor(Resource resource, ObjectName objectName) throws ArtifactsNotAvailableException
	{
		WsArtifacts result = null;
		try 
		{
			Hashtable<String, String> keyProperties = objectName.getKeyPropertyList();
			keyProperties.remove(Names.NAME_ATTRIBUTE);
			keyProperties.remove(Names.OBJECT_ID);
			
			ObjectName searchKey = ObjectName.getInstance(objectName.getDomain(),keyProperties);
			
			LOGGER.debug(
					Messages.QMAN_200041_INCOMING_OBJECT_NAME_AND_DERIVED_KEY,
					objectName,
					searchKey);
						
			result = _cache.get(searchKey);
			if (result == null)
			{
				MBeanInfo metadata = _mxServer.getMBeanInfo(objectName);
				
				WSDMArtifactsDirector director = new WSDMArtifactsDirector(objectName,metadata);
				director.setEnvironment(_environment);
				director.setResource(resource);
				director.direct();
				
				result = new WsArtifacts(
						director.getCapabilityClass(),
						director.getResourceMetadataDescriptor(),
						director.getWsdl());
				
				_cache.put(searchKey, result);

				LOGGER.debug(Messages.QMAN_200040_WS_ARTIFACTS_CACHED,searchKey);
			}
			
			return result;
		} catch(Exception exception)
		{
			throw new ArtifactsNotAvailableException(result,exception,objectName);
		}
	}
	
	/**
	 * Utility method for create concrete instance of the given capability class.
	 * 
	 * @param capabilityClass the capability class.
	 * @param objectName the object name that will act as the target for this capability invocations.
	 * @return an initialized instance of the given capability class.
	 * @throws InstantiationException when the class cannot be instantiated.
	 * @throws IllegalAccessException when this method does not have access to 
	 * 				the definition of the capability class.
	 */
	MBeanCapability createCapability(Class<MBeanCapability> capabilityClass, ObjectName objectName) 
		throws InstantiationException, IllegalAccessException
	{
		MBeanCapability capability = capabilityClass.newInstance();
		capability.setResourceObjectName(objectName);
		return capability;
	}
}