001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)ServiceAssemblyQueryImpl.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: /**
030: * ServiceAssemblyQueryImpl.java
031: *
032: * SUN PROPRIETARY/CONFIDENTIAL.
033: * This software is the proprietary information of Sun Microsystems, Inc.
034: * Use is subject to license terms.
035: *
036: * Created on August 23, 2006, 5:17 PM
037: */package com.sun.jbi.management.registry.xml;
038:
039: import java.util.List;
040: import java.util.ArrayList;
041: import java.util.Map;
042: import java.util.HashMap;
043: import java.util.logging.Logger;
044:
045: import com.sun.jbi.StringTranslator;
046: import com.sun.jbi.management.registry.Registry;
047: import com.sun.jbi.management.registry.RegistryException;
048: import com.sun.jbi.management.repository.ArchiveType;
049: import com.sun.jbi.management.LocalStringKeys;
050: import com.sun.jbi.management.system.ManagementContext;
051:
052: import com.sun.jbi.ComponentInfo;
053: import com.sun.jbi.ComponentState;
054: import com.sun.jbi.ComponentType;
055: import com.sun.jbi.ServiceUnitInfo;
056: import com.sun.jbi.ServiceAssemblyInfo;
057: import com.sun.jbi.ServiceAssemblyState;
058: import com.sun.jbi.ServiceUnitState;
059: import com.sun.jbi.management.registry.data.ServiceAssemblyInfoImpl;
060: import com.sun.jbi.management.registry.data.ServiceUnitInfoImpl;
061: import com.sun.jbi.management.descriptor.ServiceAssemblyDescriptor;
062:
063: import javax.xml.bind.JAXBException;
064:
065: /**
066: * This class encapsulates queries which are common to
067: * all targets ( domain / server / cluster ).
068: *
069: * @author Sun Microsystems, Inc.
070: */
071: public class ServiceAssemblyQueryImpl implements
072: com.sun.jbi.ServiceAssemblyQuery {
073:
074: private Jbi mJbiRegistry;
075: private Registry mRegistry;
076: private boolean mValidate;
077: private ManagementContext mMgtCtx;
078: private Logger mLogger;
079: private StringTranslator mTranslator;
080: private GenericQueryImpl mGenQuery;
081: private ObjectFactory mObjectFactory;
082: private String mTarget;
083: private ComponentQueryImpl mComponentQuery;
084:
085: private static final String DOMAIN = "domain";
086:
087: public ServiceAssemblyQueryImpl(Jbi jbi, ManagementContext mgtCtx,
088: boolean validate, String targetName, Registry registry)
089: throws RegistryException {
090: mTarget = targetName;
091: mJbiRegistry = jbi;
092: mRegistry = registry;
093: mValidate = validate;
094: mMgtCtx = mgtCtx;
095: mLogger = mMgtCtx.getLogger();
096: mTranslator = mMgtCtx.getEnvironmentContext()
097: .getStringTranslator("com.sun.jbi.management");
098:
099: mGenQuery = (GenericQueryImpl) mRegistry.getGenericQuery();
100: mComponentQuery = (ComponentQueryImpl) mRegistry
101: .getComponentQuery(mTarget);
102:
103: mObjectFactory = new ObjectFactory();
104: }
105:
106: /*--------------------------------------------------------------------------------*\
107: * Query Ops *
108: \*--------------------------------------------------------------------------------*/
109:
110: /**
111: * Get a list of all deployed service assembly names.
112: *
113: * @return A List<String> of all deployed service assemblies.
114: */
115: public List<String> getServiceAssemblies()
116:
117: {
118: List<String> serviceAssemblies = new ArrayList();
119: try {
120: if (mTarget.equals(DOMAIN)) {
121: serviceAssemblies = mGenQuery
122: .getRegisteredServiceAssemblies();
123: } else {
124:
125: InstalledComponentsListType target = null;
126: try {
127: target = mGenQuery.getInstalledEntities(mTarget);
128: } catch (RegistryException rex) {
129: throw rex;
130: }
131:
132: List<ServiceAssemblyRefType> saRefs = target
133: .getServiceAssemblyRef();
134: for (ServiceAssemblyRefType saRef : saRefs) {
135: String saName = saRef.getNameRef();
136: serviceAssemblies.add(saName);
137: }
138: }
139: } catch (RegistryException rex) {
140: mLogger.warning(rex.getMessage());
141: } finally {
142: return serviceAssemblies;
143: }
144: }
145:
146: /**
147: * Get the ServiceAssemblyInfo for a particular Service Assembly.
148: * @param serviceAssemblyName The unique name of the service assembly being retrieved.
149: * @return The ServiceAssemblyInfo for the requested service assembly or null if the
150: * service assembly is not registered.
151: */
152: public ServiceAssemblyInfo getServiceAssemblyInfo(
153: String serviceAssemblyName) {
154: try {
155: if (mGenQuery
156: .isServiceAssemblyRegistered(serviceAssemblyName)) {
157:
158: mLogger
159: .finest("Getting Service Assembly "
160: + serviceAssemblyName + " on target "
161: + mTarget);
162:
163: ServiceAssemblyInfoImpl saInfo = new ServiceAssemblyInfoImpl();
164:
165: saInfo.setName(serviceAssemblyName);
166: com.sun.jbi.management.descriptor.Jbi jbi = mGenQuery
167: .getServiceAssemblyJbi(serviceAssemblyName);
168: if (jbi == null) {
169: return (saInfo);
170: }
171:
172: ServiceAssemblyDescriptor descr = new ServiceAssemblyDescriptor(
173: jbi);
174:
175: saInfo.setDescription(descr.getDescription());
176: List<ServiceUnitInfo> resultSus = new ArrayList();
177:
178: if (mTarget != DOMAIN) {
179: List<ServiceUnitInfo> suList = descr
180: .getServiceUnits();
181: List<ServiceUnitState> suStates = new ArrayList();
182:
183: for (ServiceUnitInfo su : suList) {
184:
185: ComponentInfo compInfo = mComponentQuery
186: .getComponentInfo(su
187: .getTargetComponent());
188:
189: if (compInfo != null) {
190: List<ServiceUnitInfo> compSus = compInfo
191: .getServiceUnitList();
192: for (ServiceUnitInfo suInfo : compSus) {
193:
194: if (suInfo.getName().equals(
195: su.getName())
196: && suInfo
197: .getServiceAssemblyName()
198: .equals(
199: serviceAssemblyName)) {
200: resultSus.add(suInfo);
201: suStates.add(suInfo.getState());
202: }
203: }
204: }
205: }
206:
207: if (!suStates.isEmpty()) {
208: ServiceAssemblyState saState = ServiceAssemblyState
209: .computeServiceAssemblyState(suStates);
210: saInfo.setStatus(saState);
211: } else {
212: ServiceAssemblyRefType saRef = mGenQuery
213: .getServiceAssembly(
214: serviceAssemblyName, mTarget);
215: if (saRef != null && saRef.getState() != null) {
216: // Service assembly deployed with zero service units
217: saInfo
218: .setStatus(ServiceAssemblyState
219: .valueOfDeploymentServiceState(saRef
220: .getState().value()));
221: } else {
222: // -- Service assembly is not deployed to the target
223: saInfo
224: .setStatus(ServiceAssemblyState.UNKNOWN);
225: }
226: }
227:
228: } else {
229: // Create a Service Unit list based on information from the jbi.xml
230: List<ServiceUnitInfo> suList = descr
231: .getServiceUnits();
232:
233: for (ServiceUnitInfo su : suList) {
234: ServiceUnitInfoImpl suInfo = (ServiceUnitInfoImpl) su;
235:
236: // Set the path to the archive
237: String suRelPath = serviceAssemblyName
238: + java.io.File.separator + su.getName();
239: ;
240:
241: String suArchiveDir = mRegistry.getRepository()
242: .findArchiveDirectory(
243: ArchiveType.SERVICE_UNIT,
244: suRelPath);
245:
246: String filePath = suArchiveDir
247: + java.io.File.separator
248: + su.getTargetComponent();
249: suInfo.setFilePath(filePath);
250: resultSus.add(suInfo);
251: }
252:
253: List<ServiceAssemblyState> saStates = getAllServiceAssemblyStates(serviceAssemblyName);
254: saInfo
255: .setStatus(ServiceAssemblyState
256: .getEffectiveServiceAssemblyState(saStates));
257: }
258: saInfo.setServiceUnitList(resultSus);
259: return saInfo;
260: } else {
261: mLogger.finest("Service Assembly "
262: + serviceAssemblyName
263: + " not registered in domain");
264: }
265:
266: } catch (Exception rex) {
267: rex.printStackTrace();
268: mLogger.severe(rex.getMessage());
269: }
270: return null;
271: }
272:
273: /**
274: * Get the current status of a service assembly.
275: *
276: * @param serviceAssemblyName The unique service assembly name.
277: * @return The current status of the service assembly: ServiceAssemblyState
278: * {SHUTDOWN, STOPPED, STARTED, UNKNOWN}
279: * @throws javax.jbi.JBIException if the service assembly is not registered.
280: */
281: public com.sun.jbi.ServiceAssemblyState getStatus(
282: String serviceAssemblyName) throws javax.jbi.JBIException {
283: return getServiceAssemblyInfo(serviceAssemblyName).getStatus();
284: }
285:
286: /**
287: * @return a List conating the state of the service assembly on each server/
288: * custer in the domain.
289: * @param saName - service assembly name
290: */
291: private List<ServiceAssemblyState> getAllServiceAssemblyStates(
292: String saName) throws RegistryException {
293: List<String> servers = mGenQuery.getServers();
294: List<String> clusters = mGenQuery.getClusters();
295: List<ServiceAssemblyState> saStates = new ArrayList();
296:
297: for (String server : servers) {
298: ServiceAssemblyState saState = getServiceAssemblyStateOnTarget(
299: saName, server);
300:
301: if (!(ServiceAssemblyState.UNKNOWN == saState)) {
302: saStates.add(saState);
303: }
304: }
305:
306: for (String cluster : clusters) {
307:
308: ServiceAssemblyState saState = getServiceAssemblyStateOnTarget(
309: saName, cluster);
310:
311: if (!(ServiceAssemblyState.UNKNOWN == saState)) {
312: saStates.add(saState);
313: }
314: }
315:
316: return saStates;
317: }
318:
319: /**
320: * Query the state of the service assembly on a server / cluster.
321: *
322: * @return the service assembly state.
323: */
324: private ServiceAssemblyState getServiceAssemblyStateOnTarget(
325: String saName, String target) throws RegistryException {
326: com.sun.jbi.ServiceAssemblyQuery saQuery = mRegistry
327: .getServiceAssemblyQuery(target);
328:
329: ServiceAssemblyInfo saInfo = saQuery
330: .getServiceAssemblyInfo(saName);
331:
332: if (saInfo != null) {
333: return saInfo.getStatus();
334: }
335:
336: return ServiceAssemblyState.UNKNOWN;
337: }
338: }
|