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: * @(#)Util.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: /**
030: * Util.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 December 17, 2004, 2:26 PM
037: */package com.sun.jbi.management.system;
038:
039: import java.io.File;
040: import java.util.Properties;
041: import java.util.HashMap;
042:
043: import com.sun.jbi.management.registry.Registry;
044: import com.sun.jbi.management.registry.RegistryBuilder;
045: import com.sun.jbi.management.registry.RegistryType;
046: import com.sun.jbi.management.registry.RegistrySpecImpl;
047: import com.sun.jbi.management.registry.xml.RegistryImpl;
048: import com.sun.jbi.management.repository.Repository;
049:
050: /**
051: *
052: * @author Sun Microsystems, Inc.
053: */
054: public class Util {
055:
056: /**
057: * @param packageName is the pkgname.
058: * @return a String Translator.
059: */
060: public static com.sun.jbi.StringTranslator getStringTranslator(
061: String packageName) {
062: Class clazz;
063: java.lang.reflect.Constructor ctor;
064: try {
065: clazz = Class
066: .forName("com.sun.jbi.management.StringTranslator");
067: ctor = clazz.getDeclaredConstructors()[0];
068: ctor.setAccessible(true);
069: return (com.sun.jbi.StringTranslator) ctor
070: .newInstance(new Object[] { packageName, null });
071: //this.getClass().getClassLoader()});
072: } catch (Exception ex) {
073: ex.printStackTrace();
074: return null;
075: }
076:
077: }
078:
079: /**
080: * Make a copy of the source file in the destination file.
081: * @return true if the operation was a success, false otherwise
082: */
083: public static boolean fileCopy(String source, String destination)
084: throws java.io.IOException {
085: File sourceFile = new File(source);
086: File destFile = new File(destination);
087:
088: if (!destFile.exists()) {
089: int len = (int) sourceFile.length();
090: byte[] buf = new byte[len];
091: java.io.FileInputStream fis = new java.io.FileInputStream(
092: sourceFile);
093: fis.read(buf, 0, len);
094: if (fis != null) {
095: fis.close();
096: }
097:
098: java.io.FileOutputStream fos = new java.io.FileOutputStream(
099: destFile);
100: fos.write(buf, 0, len);
101: if (fos != null) {
102: fos.flush();
103: fos.close();
104: }
105: return true;
106: } else {
107: return false;
108: }
109: }
110:
111: /**
112: * Create a Registry Instance.
113: *
114: * @return a Registry Instance
115: * @throws Exception if an unexpected error occurs
116: */
117: public static Registry createRegistry() throws Exception {
118: return createRegistry(getRegistryDirPath(), false);
119: }
120:
121: /**
122: * Create a Registry Instance.
123: *
124: * @return a Registry Instance
125: * @throws Exception if an unexpected error occurs
126: */
127: public static Registry createRegistry(boolean readOnly)
128: throws Exception {
129: return createRegistry(getRegistryDirPath(), readOnly);
130: }
131:
132: /**
133: * Create a Registry Instance.
134: *
135: * @return a Registry Instance
136: * @throws Exception if an unexpected error occurs
137: */
138: public static Registry createRegistry(String regFolder)
139: throws Exception {
140: return createRegistry(regFolder, false);
141: }
142:
143: /**
144: * Create a Registry Instance.
145: *
146: * @return a Registry Instance
147: * @throws Exception if an unexpected error occurs
148: */
149: public static Registry createRegistry(String regFolder,
150: boolean readOnly) throws Exception {
151: try {
152:
153: // -- Create a XML Registry Spec.
154: Properties props = new Properties();
155: props.setProperty(Registry.REGISTRY_FOLDER_PROPERTY,
156: regFolder);
157: if (readOnly) {
158: props.setProperty(Registry.REGISTRY_READONLY_PROPERTY,
159: Boolean.toString(readOnly));
160: }
161: props.setProperty(Registry.REGISTRY_LOCK_INTERVAL_PROPERTY,
162: "120");
163: return RegistryBuilder
164: .buildRegistry(new RegistrySpecImpl(
165: RegistryType.XML, props,
166: createManagementContext()));
167: } catch (Exception aEx) {
168: aEx.printStackTrace();
169: throw aEx;
170: }
171: }
172:
173: /**
174: * @return the path to the Registry Folder
175: */
176: public static String getRegistryDirPath() {
177: String srcroot = System.getProperty("junit.srcroot");
178:
179: java.io.File f = new java.io.File(srcroot + "/runtime/manage");
180: if (!f.exists()) {
181: // -- mainline/whitney build
182: return srcroot + "/shasta/manage/bld/regress/testdata";
183: } else {
184: // -- open-esb build
185: return srcroot
186: + "/runtime/manage/bld/test-classes/testdata";
187: }
188: }
189:
190: public static ManagementContext createManagementContext()
191: throws Exception {
192: ScaffoldEnvironmentContext envCtx = new ScaffoldEnvironmentContext();
193: com.sun.jbi.util.EnvironmentAccess.setContext(envCtx);
194: String testPath = System.getProperty("junit.srcroot")
195: + "/runtime/manage/bld/";
196: envCtx.setJbiInstanceRoot(testPath);
197: envCtx.setAppServerInstanceRoot(testPath);
198: envCtx.setJbiInstallRoot(System.getProperty("junit.as8base")
199: + "/jbi");
200: ManagementContext ctx = new ManagementContext(envCtx);
201: Repository repository = new Repository(ctx);
202:
203: ctx.setRepository(repository);
204:
205: return ctx;
206:
207: }
208: }
|