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: * @(#)ObjectTranslator.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: /**
030: * ObjectTranslator.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.ArrayList;
040: import java.util.List;
041: import java.util.Map;
042:
043: import com.sun.jbi.ComponentInfo;
044: import com.sun.jbi.ComponentState;
045: import com.sun.jbi.ComponentType;
046: import com.sun.jbi.ServiceUnitInfo;
047: import com.sun.jbi.ServiceUnitState;
048: import com.sun.jbi.platform.PlatformContext;
049: import javax.xml.bind.JAXBException;
050:
051: import com.sun.jbi.management.registry.RegistryException;
052: import com.sun.jbi.management.registry.data.ComponentInfoImpl;
053: import com.sun.jbi.management.registry.data.ServiceUnitInfoImpl;
054: import com.sun.jbi.management.descriptor.ComponentDescriptor;
055: import com.sun.jbi.management.descriptor.SharedLibraryDescriptor;
056: import com.sun.jbi.management.util.PropertyFilter;
057: import com.sun.jbi.management.util.StringHelper;
058:
059: /**
060: * Helper class to translate from JAXB objects to Registry objects.
061: *
062: * @author Sun Microsystems, Inc.
063: */
064: public class ObjectTranslator {
065: private static ObjectFactory sObjectFactory;
066: private static String NA = "NA";
067:
068: static {
069:
070: sObjectFactory = new ObjectFactory();
071: }
072:
073: public static SharedLibraryRefType getJaxbSharedLibraryRef(
074: ComponentInfo slInfo) throws JAXBException {
075: SharedLibraryRefType slRef = sObjectFactory
076: .createSharedLibraryRefType();
077: slRef.setNameRef(StringHelper.trim(slInfo.getName()));
078: slRef.setInstallRoot(getRelativePath(StringHelper.trim(slInfo
079: .getInstallRoot())));
080: return slRef;
081: }
082:
083: public static ServiceAssemblyRefType getJaxbServiceAssemblyRef(
084: String saName) throws JAXBException {
085: ServiceAssemblyRefType saRef = sObjectFactory
086: .createServiceAssemblyRefType();
087: saRef.setNameRef(StringHelper.trim(saName));
088: return saRef;
089: }
090:
091: public static ComponentRefType getJaxbComponentRef(
092: ComponentInfo compInfo) throws JAXBException {
093: ComponentRefType compRef = sObjectFactory
094: .createComponentRefType();
095:
096: compRef.setNameRef(StringHelper.trim(compInfo.getName()));
097: compRef.setInstallRoot(getRelativePath(StringHelper
098: .trim(compInfo.getInstallRoot())));
099: compRef.setServiceUnits(getJaxbServiceUnitListType(compInfo
100: .getServiceUnitList()));
101: compRef.setState(LifeCycleStatusEnum.fromValue(compInfo
102: .getStatus().toString()));
103: compRef.setWorkspace(getRelativePath(StringHelper.trim(compInfo
104: .getWorkspaceRoot())));
105: updateJaxbComponentPropertyList(compInfo.getProperties(),
106: compRef.getProperty());
107: return compRef;
108: }
109:
110: public static ServiceUnitListType getJaxbServiceUnitListType(
111: List<ServiceUnitInfo> suInfoList) throws JAXBException {
112: ServiceUnitListType suListType = sObjectFactory
113: .createServiceUnitListType();
114:
115: for (ServiceUnitInfo suInfo : suInfoList) {
116: ServiceUnitType suType = getJaxbServiceUnitType(suInfo);
117: suListType.getServiceUnit().add(suType);
118: }
119: return suListType;
120: }
121:
122: public static ServiceUnitType getJaxbServiceUnitType(
123: ServiceUnitInfo suInfo) throws JAXBException {
124: ServiceUnitType suType = sObjectFactory.createServiceUnitType();
125:
126: suType.setName(StringHelper.trim(suInfo.getName()));
127: suType.setServiceAssemblyRef(StringHelper.trim(suInfo
128: .getServiceAssemblyName()));
129: suType.setState(LifeCycleStatusEnum.fromValue(suInfo.getState()
130: .toString()));
131:
132: return suType;
133: }
134:
135: public static void updateJaxbComponentPropertyList(Map compProps,
136: List<PropertyType> list) throws JAXBException {
137: java.util.Iterator itr = compProps.keySet().iterator();
138: while (itr.hasNext()) {
139: PropertyType pt = sObjectFactory.createPropertyType();
140:
141: String key = (String) itr.next();
142: pt.setName(StringHelper.trim(key));
143: pt.setValue(StringHelper.trim((String) compProps.get(key)));
144:
145: list.add(pt);
146: }
147: }
148:
149: /*---------------------------------------------------------------------------------*\
150: * Operations to convert JAXB Objects to the Registry data objects *
151: \*---------------------------------------------------------------------------------*/
152:
153: public static ComponentInfo getRegistryComponent(
154: ComponentRefType compRef, GenericQueryImpl genQuery)
155: throws JAXBException, RegistryException {
156: ComponentInfoImpl compInfo = null;
157:
158: if (compRef != null) {
159: String compName = StringHelper.trim(compRef.getNameRef());
160: String compInstallRoot = StringHelper.trim(compRef
161: .getInstallRoot());
162: compInfo = new ComponentInfoImpl();
163:
164: compInfo.setName(compName);
165: compInfo.setInstallRoot(getAbsolutePath(compInstallRoot));
166: compInfo.setStatus(ComponentState
167: .valueOfString(StringHelper.trim(compRef.getState()
168: .value())));
169: compInfo.setWorkspaceRoot(getAbsolutePath(StringHelper
170: .trim(compRef.getWorkspace())));
171: compInfo.setTimestamp(genQuery
172: .getComponentTimestamp(compName));
173: compInfo.setUpgradeNumber(genQuery
174: .getComponentUpgradeNumber(compName).intValue());
175: compInfo.setServiceUnitList(getRegistryServiceUnitListType(
176: compRef.getServiceUnits(), StringHelper
177: .trim(compRef.getNameRef())));
178: updateRegistryComponentPropertyList(compInfo
179: .getProperties(), compRef.getProperty());
180: updateRegistryComponentConfigInfo(compInfo, compRef
181: .getComponentConfig());
182: compInfo = updateComponentWithDescriptorData(compInfo,
183: genQuery);
184:
185: }
186: return compInfo;
187: }
188:
189: /**
190: * This operation is called for target="domain"
191: */
192: public static ComponentInfo getRegistryComponent(
193: String componentName, GenericQueryImpl genQuery)
194: throws JAXBException, RegistryException {
195: ComponentInfoImpl compInfo = null;
196:
197: if (componentName != null) {
198: compInfo = new ComponentInfoImpl();
199:
200: compInfo.setName(componentName);
201: compInfo.setInstallRoot(NA);
202: compInfo.setStatus(ComponentState.UNKNOWN);
203: compInfo.setWorkspaceRoot(NA);
204: compInfo.setTimestamp(genQuery
205: .getComponentTimestamp(componentName));
206: compInfo.setUpgradeNumber(genQuery
207: .getComponentUpgradeNumber(componentName)
208: .intValue());
209: compInfo.setServiceUnitList(new ArrayList());
210:
211: compInfo = updateComponentWithDescriptorData(compInfo,
212: genQuery);
213: }
214: return compInfo;
215: }
216:
217: public static ComponentInfo getRegistrySharedLibrary(
218: GenericQueryImpl genQuery, SharedLibraryRefType slRef)
219: throws RegistryException {
220: ComponentInfoImpl slInfo = null;
221:
222: if (slRef != null) {
223: String slName = slRef.getNameRef();
224:
225: slInfo = new ComponentInfoImpl();
226:
227: slInfo.setName(slName);
228: slInfo.setInstallRoot(getAbsolutePath(slRef
229: .getInstallRoot()));
230: slInfo.setStatus(ComponentState.SHUTDOWN);
231: slInfo.setComponentType(ComponentType.SHARED_LIBRARY);
232: slInfo.setTimestamp(genQuery.getComponentTimestamp(slName));
233: slInfo = updateSharedLibraryWithDescriptorData(slInfo,
234: genQuery);
235: }
236: return slInfo;
237: }
238:
239: public static ComponentInfo getRegistrySharedLibrary(
240: GenericQueryImpl genQuery, String slName)
241: throws RegistryException {
242: ComponentInfoImpl slInfo = new ComponentInfoImpl();
243:
244: slInfo.setName(slName);
245: slInfo.setInstallRoot(NA);
246: slInfo.setStatus(ComponentState.SHUTDOWN);
247: slInfo.setComponentType(ComponentType.SHARED_LIBRARY);
248: slInfo.setTimestamp(genQuery.getComponentTimestamp(slName));
249: slInfo = updateSharedLibraryWithDescriptorData(slInfo, genQuery);
250: return slInfo;
251: }
252:
253: private static List<String> getClassPathList(String installRoot,
254: List<String> classPathList) {
255: List<String> absCp = new ArrayList();
256:
257: for (String classpath : classPathList) {
258: absCp.add(installRoot + java.io.File.separator + classpath);
259: }
260:
261: return absCp;
262: }
263:
264: private static List<ServiceUnitInfo> getRegistryServiceUnitListType(
265: ServiceUnitListType suList, String targetComponent) {
266: List suInfoList = new java.util.ArrayList();
267:
268: if (suList != null) {
269: List<ServiceUnitType> sus = suList.getServiceUnit();
270:
271: for (ServiceUnitType suType : sus) {
272: ServiceUnitInfo suInfo = getRegistryServiceUnitType(
273: suType, targetComponent);
274: suInfoList.add(suInfo);
275: }
276: }
277: return suInfoList;
278: }
279:
280: private static void updateRegistryComponentPropertyList(
281: Map compProps, List<PropertyType> list)
282: throws JAXBException {
283: for (PropertyType pt : list) {
284: compProps.put(pt.getName(), pt.getValue());
285: }
286: }
287:
288: /**
289: * Populate the Component Info with all the configuration information ( i.e.
290: * all = static config, env vars and named configs )
291: */
292: private static void updateRegistryComponentConfigInfo(
293: ComponentInfoImpl compInfo, ComponentConfigType compCfg) {
294: if (compCfg != null) {
295: // Populate the static configuration
296: List<PropertyType> regProps = compCfg.getProperty();
297: java.util.Properties props = new java.util.Properties();
298:
299: for (PropertyType regProp : regProps) {
300: props.put(regProp.getName(), regProp.getValue());
301: }
302: compInfo.setConfiguration(props);
303:
304: // Populate the application variables
305: List<AppVariableType> appVars = compCfg
306: .getApplicationVariable();
307: com.sun.jbi.management.ComponentInfo.Variable[] vars = new com.sun.jbi.management.ComponentInfo.Variable[appVars
308: .size()];
309:
310: int i = 0;
311: for (AppVariableType appVar : appVars) {
312: vars[i++] = new com.sun.jbi.management.ComponentInfo.Variable(
313: appVar.getName(), appVar.getValue(), appVar
314: .getType());
315: }
316: compInfo.setVariables(vars);
317:
318: // populate the named configurations
319: List<AppConfigType> appConfigs = compCfg
320: .getApplicationConfiguration();
321: for (AppConfigType appConfig : appConfigs) {
322: List<PropertyType> cfgProps = appConfig.getProperty();
323:
324: java.util.Properties appCfgProps = new java.util.Properties();
325:
326: for (PropertyType cfgrop : cfgProps) {
327: appCfgProps
328: .put(cfgrop.getName(), cfgrop.getValue());
329: }
330: compInfo
331: .setApplicationConfiguration(
332: appCfgProps
333: .getProperty(com.sun.jbi.management.registry.Registry.APP_CONFIG_NAME_KEY),
334: appCfgProps);
335: }
336: }
337: }
338:
339: private static ServiceUnitInfo getRegistryServiceUnitType(
340: ServiceUnitType suType, String targetComponent) {
341: ServiceUnitInfoImpl suInfo = new ServiceUnitInfoImpl();
342:
343: suInfo.setName(StringHelper.trim(suType.getName()));
344: suInfo.setTargetComponent(targetComponent);
345: suInfo.setServiceAssemblyName(StringHelper.trim(suType
346: .getServiceAssemblyRef()));
347: suInfo.setState(ServiceUnitState.valueOfString(StringHelper
348: .trim(suType.getState().value())));
349:
350: return suInfo;
351: }
352:
353: /**
354: * Get the Component data from jbi.xml and update the registry component
355: * information
356: */
357: private static ComponentInfoImpl updateComponentWithDescriptorData(
358: ComponentInfoImpl compInfo, GenericQueryImpl genQuery)
359: throws RegistryException {
360: String compName = compInfo.getName();
361: String compInstallRoot = StringHelper.trim(compInfo
362: .getInstallRoot());
363: String compRoot = "";
364: if (!NA.equals(compInstallRoot)) {
365: compRoot = getAbsolutePath(compInstallRoot);
366: }
367:
368: // -- Get the data from the components jbi.xml
369: compInfo.setInstallationDescriptor(genQuery
370: .getComponentInstallationDescriptor(compName));
371: com.sun.jbi.management.descriptor.Jbi jbi = genQuery
372: .getComponentJbi(compName);
373:
374: if (jbi != null) {
375: ComponentDescriptor descr = new ComponentDescriptor(jbi);
376:
377: String compClass = descr.getComponentClassName();
378: String bootClass = descr.getBootstrapClassName();
379: compInfo.setComponentClassName(compClass);
380: compInfo.setBootstrapClassName(bootClass);
381:
382: compInfo.setComponentType(descr.getComponentType());
383: compInfo.setDescription(descr.getDescription());
384:
385: compInfo.setClassLoaderSelfFirst(descr
386: .isComponentClassLoaderSelfFirst());
387: compInfo.setBootstrapClassLoaderSelfFirst(descr
388: .isBootstrapClassLoaderSelfFirst());
389:
390: // -- Get the shared libraries
391: compInfo.setSharedLibraryNames(descr.getSharedLibraryIds());
392:
393: compInfo.setClassPathElements(getClassPathList(compRoot,
394: descr.getComponentClassPathElements()));
395: compInfo.setBootstrapClassPathElements(getClassPathList(
396: compRoot, descr.getBootstrapClassPathElements()));
397: }
398: return compInfo;
399: }
400:
401: private static ComponentInfoImpl updateSharedLibraryWithDescriptorData(
402: ComponentInfoImpl slInfo, GenericQueryImpl genQuery)
403: throws RegistryException {
404: String slName = slInfo.getName();
405: String slInstallRoot = StringHelper.trim(slInfo
406: .getInstallRoot());
407: String slRoot = "";
408: if (!NA.equals(slInstallRoot)) {
409: slRoot = getAbsolutePath(slInstallRoot);
410: }
411:
412: // -- jbi.xml content
413: slInfo.setInstallationDescriptor(genQuery
414: .getSharedLibraryInstallationDescriptor(slName));
415: com.sun.jbi.management.descriptor.Jbi jbi = genQuery
416: .getSharedLibraryJbi(slName);
417:
418: if (jbi != null) {
419: SharedLibraryDescriptor descr = new SharedLibraryDescriptor(
420: jbi);
421:
422: slInfo.setDescription(descr.getDescription());
423: slInfo.setClassLoaderSelfFirst(descr
424: .isSharedLibraryClassLoaderSelfFirst());
425: // .. classpath .. InstallRoot + classpath entry in jbi.xml
426: slInfo.setClassPathElements(getClassPathList(slRoot, descr
427: .getSharedLibraryClassPathElements()));
428: }
429:
430: return slInfo;
431: }
432:
433: /**
434: * This operation parses the path string and replaces any occurences of
435: * the value of a system property.
436: */
437: private static String getRelativePath(String path) {
438: String resStr = PropertyFilter.replacePropertyValues(path,
439: PlatformContext.INSTANCE_ROOT_TOKEN);
440: resStr = PropertyFilter.replacePropertyValues(resStr,
441: PlatformContext.INSTALL_ROOT_TOKEN);
442: return resStr;
443: }
444:
445: /**
446: * This operation parses the path string and replaces any occurences of
447: * a system property with it's actual value.
448: */
449: private static String getAbsolutePath(String path) {
450: return PropertyFilter.filterProperties(path);
451: }
452: }
|