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: * @(#)ComponentQueryImpl.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: /**
030: * ComponentQueryImpl.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.data.ServiceUnitInfoImpl;
047: import com.sun.jbi.management.registry.Registry;
048: import com.sun.jbi.management.registry.RegistryException;
049: import com.sun.jbi.management.repository.ArchiveType;
050: import com.sun.jbi.management.LocalStringKeys;
051: import com.sun.jbi.management.system.ManagementContext;
052: import com.sun.jbi.management.util.LockManager;
053:
054: import com.sun.jbi.ComponentInfo;
055: import com.sun.jbi.ComponentState;
056: import com.sun.jbi.ComponentType;
057: import com.sun.jbi.ServiceUnitInfo;
058: import com.sun.jbi.ServiceAssemblyInfo;
059: import com.sun.jbi.ServiceUnitState;
060:
061: import javax.xml.bind.JAXBException;
062: import javax.jbi.component.Component;
063:
064: /**
065: * This class encapsulates queries which are common to
066: * all targets ( domain / server / cluster ).
067: *
068: * @author Sun Microsystems, Inc.
069: */
070: public class ComponentQueryImpl implements com.sun.jbi.ComponentQuery {
071:
072: private Jbi mJbiRegistry;
073: private RegistryImpl mRegistry;
074: private boolean mValidate;
075: private ManagementContext mMgtCtx;
076: private Logger mLogger;
077: private StringTranslator mTranslator;
078: private GenericQueryImpl mGenQuery;
079: private ObjectFactory mObjectFactory;
080: private String mTarget;
081: private Component mComponentInstance;
082: private LockManager mRegObjLM;
083:
084: private static String TARGET_DOMAIN = "domain";
085:
086: public ComponentQueryImpl(Jbi jbi, ManagementContext mgtCtx,
087: boolean validate, String targetName, RegistryImpl registry)
088: throws RegistryException {
089: mTarget = targetName;
090: mJbiRegistry = jbi;
091: mRegistry = registry;
092: mValidate = validate;
093: mMgtCtx = mgtCtx;
094: mLogger = mMgtCtx.getLogger();
095: mTranslator = mMgtCtx.getEnvironmentContext()
096: .getStringTranslator("com.sun.jbi.management");
097:
098: mGenQuery = (GenericQueryImpl) mRegistry.getGenericQuery();
099:
100: mObjectFactory = new ObjectFactory();
101:
102: mRegObjLM = registry.getRegistryObjectLockManager();
103: }
104:
105: /*--------------------------------------------------------------------------------*\
106: * Query Ops *
107: \*--------------------------------------------------------------------------------*/
108:
109: /**
110: * Get a list of component IDs for all registered components of a specified
111: * type.
112: * @param type The ComponentType: { BINDING, ENGINE, SHARED_LIBRARY,
113: * BINDINGS_AND_ENGINES, ALL }. ALL includes allcomponents regardless of their types.
114: * ComponentInfo.BINDINGS_AND_ENGINES includes both bindings and engines.
115: * @return A List of component IDs ( String ) of all registered components
116: * of the requested type. Returns an empty list of no components were found.
117: */
118: public List<String> getComponentIds(ComponentType type) {
119: List<String> list = new ArrayList();
120:
121: try {
122: switch (type) {
123: case ALL:
124: list = getSharedLibraries(getAllComponents());
125: break;
126: case BINDINGS_AND_ENGINES:
127: list = getAllComponents();
128: break;
129: case BINDING:
130: list = getBindings();
131: break;
132: case ENGINE:
133: list = getEngines();
134: break;
135: case SHARED_LIBRARY:
136: list = getSharedLibraries(list);
137: break;
138: }
139:
140: } catch (RegistryException rex) {
141: mLogger.severe(rex.getMessage());
142: }
143:
144: return list;
145: }
146:
147: /**
148: * Get a list of component IDs for all components of a specified type with
149: * a specified status.
150: * @param type The ComponentType: { BINDING, ENGINE, SHARED_LIBRARY,
151: * BINDINGS_AND_ENGINES, ALL }. ALL includes allcomponents regardless of their types.
152: * ComponentInfo.BINDINGS_AND_ENGINES includes both bindings and engines.
153: * @param status The Component status: ComponentState{LOADED, SHUTDOWN, STARTED,
154: * STOPPED}
155: * @return A List of component IDs ( String ) of all registered components
156: * of the requested type with the requested status. Returns an empty list if
157: * no components were found.
158: */
159: public List<String> getComponentIds(ComponentType type,
160: ComponentState status) {
161: List<String> list = new ArrayList();
162:
163: try {
164: switch (type) {
165: case ALL:
166: list = getSharedLibraries(getAllComponents(status),
167: status);
168: break;
169: case BINDINGS_AND_ENGINES:
170: list = getAllComponents(status);
171: break;
172: case BINDING:
173: list = getBindings(status);
174: break;
175: case ENGINE:
176: list = getEngines(status);
177: break;
178: case SHARED_LIBRARY:
179: list = getSharedLibraries(list, status);
180: break;
181: }
182:
183: } catch (RegistryException rex) {
184: mLogger.severe(rex.getMessage());
185: }
186:
187: return list;
188: }
189:
190: /**
191: * Get the ComponentInfo for a particular Component.
192: * @param componentName The unique name of the component being retrieved.
193: * @return The ComponentInfo for the requested component or null if the
194: * component is not registered.
195: */
196: public ComponentInfo getComponentInfo(String componentName) {
197:
198: try {
199: mLogger
200: .finest("Registry retrieving component information for "
201: + componentName + " on target " + mTarget);
202: if (mGenQuery.isComponentRegistered(componentName)) {
203: com.sun.jbi.ComponentInfo compInfo = null;
204: mRegObjLM.acquireReadLock();
205: try {
206: if (TARGET_DOMAIN.equals(mTarget)) {
207: compInfo = ObjectTranslator
208: .getRegistryComponent(componentName,
209: mGenQuery);
210:
211: ((com.sun.jbi.management.registry.data.ComponentInfoImpl) compInfo)
212: .setStatus(getStatus(componentName));
213: } else {
214: ComponentRefType compRef = mGenQuery
215: .getComponent(componentName, mTarget);
216: compInfo = ObjectTranslator
217: .getRegistryComponent(compRef,
218: mGenQuery);
219: // -- Update the Service Unit file paths
220: if (compInfo != null) {
221: updateServiceUnits(compInfo
222: .getServiceUnitList(),
223: componentName);
224: }
225: }
226: } finally {
227: mRegObjLM.releaseReadLock();
228: return compInfo;
229: }
230: } else {
231: mLogger.finest("Component " + componentName
232: + " not registered in domain");
233: }
234:
235: } catch (Exception rex) {
236: rex.printStackTrace();
237: mLogger.severe(rex.getMessage());
238: }
239: return null;
240: }
241:
242: /**
243: * Get the ComponentInfo for a particular Shared Library.
244: * @param sharedLibraryName The unique name of the Shared Library being
245: * retrieved.
246: * @return The ComponentInfo for the requested Shared Library or null if the
247: * Shared Library is not registered.
248: */
249: public ComponentInfo getSharedLibraryInfo(String sharedLibraryName) {
250: try {
251: if (mGenQuery.isSharedLibraryRegistered(sharedLibraryName)) {
252: if (TARGET_DOMAIN.equals(mTarget)) {
253: ComponentInfo slInfo = ObjectTranslator
254: .getRegistrySharedLibrary(mGenQuery,
255: sharedLibraryName);
256: return slInfo;
257: } else {
258: ComponentInfo slInfo = null;
259: mRegObjLM.acquireReadLock();
260: try {
261: SharedLibraryRefType slRef = mGenQuery
262: .getSharedLibrary(sharedLibraryName,
263: mTarget);
264: slInfo = ObjectTranslator
265: .getRegistrySharedLibrary(mGenQuery,
266: slRef);
267: } finally {
268: mRegObjLM.releaseReadLock();
269: }
270: return slInfo;
271: }
272: }
273:
274: } catch (RegistryException rex) {
275: mLogger.severe(rex.getMessage());
276: }
277: return null;
278: }
279:
280: /**
281: * Get a list of component IDs that depend upon a specified Shared Library.
282: * @param sharedLibraryName The unique name of the Shared Library.
283: * @return A list of the component IDs of all components that depend upon the
284: * Shared Library. If none are found, the list is empty.
285: */
286: public List getDependentComponentIds(String sharedLibraryName) {
287:
288: List<String> depComponents = new ArrayList();
289:
290: try {
291: if (mGenQuery.isSharedLibraryRegistered(sharedLibraryName)) {
292: List<String> allComponents = getAllComponents();
293:
294: for (String component : allComponents) {
295: com.sun.jbi.management.descriptor.Jbi jbi = mGenQuery
296: .getComponentJbi(component);
297:
298: List<com.sun.jbi.management.descriptor.Component.SharedLibrary> slList = jbi
299: .getComponent().getSharedLibraryList();
300:
301: for (com.sun.jbi.management.descriptor.Component.SharedLibrary sl : slList) {
302: if (sl.getContent().equals(sharedLibraryName)) {
303: depComponents.add(component);
304: }
305: }
306: }
307: }
308: } catch (RegistryException rex) {
309: mLogger.severe(rex.getMessage());
310: }
311: return depComponents;
312: }
313:
314: /**
315: * Get the current status of a component.
316: * @param componentName The unique component name.
317: * @return The current status of the component: {SHUTDOWN, LOADED, STARTED, STOPPED}
318: * @throws javax.jbi.JBIException if the component name is not registered.
319: */
320: public ComponentState getStatus(String componentName)
321: throws javax.jbi.JBIException {
322: if (TARGET_DOMAIN.equals(mTarget)) {
323: List<String> targets = mGenQuery
324: .getServersInstallingComponent(componentName);
325: targets.addAll(mGenQuery
326: .getClustersInstallingComponent(componentName));
327: List<ComponentState> states = new ArrayList<ComponentState>();
328: for (String target : targets) {
329: states.add(mGenQuery.getComponentState(componentName,
330: target));
331: }
332: return ComponentState.computeEffectiveState(states);
333: } else {
334: return mGenQuery.getComponentState(componentName, mTarget);
335: }
336: }
337:
338: /*----------------------------------------------------------------------------------*\
339: * Private Helpers *
340: \*----------------------------------------------------------------------------------*/
341:
342: private List<String> getAllComponents() throws RegistryException {
343: List<String> components = new ArrayList();
344: List<String> compNames = new ArrayList();
345:
346: if (!TARGET_DOMAIN.equals(mTarget)) {
347: mRegObjLM.acquireReadLock();
348: try {
349:
350: InstalledComponentsListType target = null;
351: try {
352: target = getTarget(mTarget);
353: } catch (RegistryException rex) {
354: throw rex;
355: }
356:
357: if (target != null) {
358: List<ComponentRefType> compRefs = target
359: .getComponentRef();
360: for (ComponentRefType compRef : compRefs) {
361: String compName = compRef.getNameRef();
362: compNames.add(compName);
363: }
364: }
365: } finally {
366: mRegObjLM.releaseReadLock();
367: }
368:
369: } else {
370: mRegObjLM.acquireReadLock();
371: try {
372: List<DomainComponentType> compList = getDomainComponents();
373: for (DomainComponentType comp : compList) {
374: String compName = comp.getName();
375: compNames.add(compName);
376: }
377: } finally {
378: mRegObjLM.releaseReadLock();
379: }
380: }
381:
382: for (String compName : compNames) {
383: if (mGenQuery.isComponentRegistered(compName)) {
384: components.add(compName);
385: }
386: }
387: return components;
388: }
389:
390: private List<String> getBindings() throws RegistryException {
391: List<String> components = getAllComponents();
392: List<String> bindings = new ArrayList();
393:
394: for (String compName : components) {
395:
396: if (mGenQuery.isComponentRegistered(compName)
397: && mGenQuery.getComponentType(compName) == ComponentType.BINDING) {
398: bindings.add(compName);
399: }
400: }
401:
402: return bindings;
403: }
404:
405: private List<String> getEngines() throws RegistryException {
406: List<String> components = getAllComponents();
407: List<String> engines = new ArrayList();
408:
409: for (String compName : components) {
410: if (mGenQuery.isComponentRegistered(compName)
411: && mGenQuery.getComponentType(compName) == ComponentType.ENGINE) {
412: engines.add(compName);
413: }
414: }
415:
416: return engines;
417: }
418:
419: private List<String> getSharedLibraries(List<String> list)
420: throws RegistryException {
421: if (list == null) {
422: list = new ArrayList();
423: }
424: List<String> slNames = new ArrayList();
425:
426: if (!TARGET_DOMAIN.equals(mTarget)) {
427: InstalledComponentsListType target = null;
428:
429: mRegObjLM.acquireReadLock();
430: try {
431: target = getTarget(mTarget);
432:
433: if (target != null) {
434: List<SharedLibraryRefType> slRefs = target
435: .getSharedLibraryRef();
436: for (SharedLibraryRefType slRef : slRefs) {
437: String slName = slRef.getNameRef();
438: slNames.add(slName);
439: }
440: }
441: } catch (RegistryException rex) {
442: throw rex;
443: } finally {
444: mRegObjLM.releaseReadLock();
445: }
446: } else {
447: mRegObjLM.acquireReadLock();
448: try {
449: List<DomainSharedLibraryType> slList = getDomainSharedLibraries();
450: for (DomainSharedLibraryType sl : slList) {
451: String slName = sl.getName();
452: slNames.add(slName);
453: }
454: } finally {
455: mRegObjLM.releaseReadLock();
456: }
457: }
458:
459: for (String slName : slNames) {
460: if (mGenQuery.isSharedLibraryRegistered(slName)) {
461: list.add(slName);
462: }
463: }
464: return list;
465: }
466:
467: private List<String> getSharedLibraries(List<String> list,
468: ComponentState status) throws RegistryException {
469: if (list == null) {
470: list = new ArrayList();
471: }
472: List<String> slNames = new ArrayList();
473:
474: // -- Add shared libraries only if status is SHUTDOWN
475:
476: if (!(TARGET_DOMAIN.equals(mTarget))) {
477: if (ComponentState.SHUTDOWN == status) {
478: InstalledComponentsListType target = null;
479: mRegObjLM.acquireReadLock();
480: try {
481: target = getTarget(mTarget);
482: if (target != null) {
483: List<SharedLibraryRefType> slRefs = target
484: .getSharedLibraryRef();
485: for (SharedLibraryRefType slRef : slRefs) {
486: String slName = slRef.getNameRef();
487: slNames.add(slName);
488: }
489: }
490: } catch (RegistryException rex) {
491: throw rex;
492: } finally {
493: mRegObjLM.releaseReadLock();
494: }
495:
496: for (String slName : slNames) {
497: if (mGenQuery.isSharedLibraryRegistered(slName)) {
498: list.add(slName);
499: }
500: }
501:
502: }
503: } else {
504: // -- status is not considered for the domain target
505: return getSharedLibraries(list);
506: }
507: return list;
508: }
509:
510: private List<String> getAllComponents(ComponentState status)
511: throws RegistryException {
512:
513: List<String> components = new ArrayList();
514: List<String> compNames = new ArrayList();
515:
516: if (!(TARGET_DOMAIN.equals(mTarget))) {
517:
518: InstalledComponentsListType target = null;
519: mRegObjLM.acquireReadLock();
520: try {
521: target = getTarget(mTarget);
522: if (target != null) {
523: List<ComponentRefType> compRefs = target
524: .getComponentRef();
525: for (ComponentRefType compRef : compRefs) {
526: ComponentState state = ComponentState
527: .valueOfString(compRef.getState()
528: .value());
529: if (state.equals(status)) {
530: String compName = compRef.getNameRef();
531: compNames.add(compName);
532: }
533: }
534: }
535: } catch (RegistryException rex) {
536: throw rex;
537: } finally {
538: mRegObjLM.releaseReadLock();
539: }
540:
541: for (String compName : compNames) {
542: if (mGenQuery.isComponentRegistered(compName)) {
543: components.add(compName);
544: }
545: }
546: } else {
547: // -- State doesn't matter for "domain" target
548: components = getAllComponents();
549: }
550:
551: return components;
552: }
553:
554: private List<String> getBindings(ComponentState status)
555: throws RegistryException {
556: List<String> components = getAllComponents(status);
557: List<String> bindings = new ArrayList();
558:
559: for (String compName : components) {
560: if (mGenQuery.getComponentType(compName) == ComponentType.BINDING) {
561: bindings.add(compName);
562: }
563: }
564:
565: return bindings;
566: }
567:
568: private List<String> getEngines(ComponentState status)
569: throws RegistryException {
570: List<String> components = getAllComponents(status);
571: List<String> engines = new ArrayList();
572:
573: for (String compName : components) {
574: if (mGenQuery.getComponentType(compName) == ComponentType.ENGINE) {
575: engines.add(compName);
576: }
577: }
578:
579: return engines;
580: }
581:
582: private InstalledComponentsListType getTarget(String targetName)
583: throws RegistryException {
584: InstalledComponentsListType target = null;
585:
586: if (mGenQuery.isTargetServer(targetName)) {
587: List<InstalledComponentsListType> servers = mJbiRegistry
588: .getServers().getServer();
589:
590: for (InstalledComponentsListType server : servers) {
591: if (server.getNameRef().equals(targetName)) {
592: target = server;
593: }
594: }
595: } else if (mGenQuery.isTargetCluster(targetName)) {
596: List<InstalledComponentsListType> clusters = mJbiRegistry
597: .getClusters().getCluster();
598:
599: for (InstalledComponentsListType cluster : clusters) {
600: if (cluster.getNameRef().equals(targetName)) {
601: target = cluster;
602: }
603: }
604: }
605: if (target == null) {
606: mLogger.finest(mTranslator.getString(
607: LocalStringKeys.JBI_ADMIN_UNKNOWN_TARGET, mTarget));
608: }
609: return target;
610: }
611:
612: private List<DomainComponentType> getDomainComponents()
613: throws RegistryException {
614: Components compType = mJbiRegistry.getComponents();
615:
616: if (compType != null) {
617: return compType.getComponent();
618: } else {
619: return new ArrayList();
620: }
621: }
622:
623: private List<DomainSharedLibraryType> getDomainSharedLibraries()
624: throws RegistryException {
625:
626: SharedLibraries slType = mJbiRegistry.getSharedLibraries();
627:
628: if (slType != null) {
629: return slType.getSharedLibrary();
630: } else {
631: return new ArrayList();
632: }
633: }
634:
635: private List<DomainEntityType> getDomainServiceAssemblies()
636: throws RegistryException {
637: ServiceAssemblies saType = mJbiRegistry.getServiceAssemblies();
638:
639: if (saType != null) {
640: return saType.getServiceAssembly();
641: } else {
642: return new ArrayList();
643: }
644: }
645:
646: /**
647: * For each ServiceUnitInfo in the list add the path to the Service Unit Root.
648: *
649: * @param suList - List of ServiceUnitInfos, which need to be updated
650: * @param componentName - the target component of the ServiceUnitInfos
651: */
652: private void updateServiceUnits(List<ServiceUnitInfo> suList,
653: String componentName) {
654: for (ServiceUnitInfo suInfo : suList) {
655: String suRelPath = suInfo.getServiceAssemblyName()
656: + java.io.File.separator + suInfo.getName();
657: String suArchiveDir = mRegistry
658: .getRepository()
659: .findArchiveDirectory(
660: com.sun.jbi.management.repository.ArchiveType.SERVICE_UNIT,
661: suRelPath);
662:
663: String filePath = suArchiveDir + java.io.File.separator
664: + componentName;
665: ((ServiceUnitInfoImpl) suInfo).setFilePath(filePath);
666: }
667: }
668: }
|