001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.assembler.classic.cmd;
017:
018: import org.apache.commons.cli.CommandLine;
019: import org.apache.commons.cli.CommandLineParser;
020: import org.apache.commons.cli.HelpFormatter;
021: import org.apache.commons.cli.Option;
022: import org.apache.commons.cli.OptionBuilder;
023: import org.apache.commons.cli.Options;
024: import org.apache.commons.cli.ParseException;
025: import org.apache.commons.cli.PosixParser;
026: import org.apache.openejb.assembler.classic.OpenEjbConfiguration;
027: import org.apache.openejb.assembler.classic.ServiceInfo;
028: import org.apache.openejb.loader.SystemInstance;
029: import org.apache.openejb.util.Messages;
030: import org.apache.openejb.util.OpenEjbVersion;
031: import org.apache.openejb.util.URISupport;
032: import org.apache.openejb.util.SuperProperties;
033:
034: import javax.naming.Context;
035: import javax.naming.InitialContext;
036: import java.io.File;
037: import java.io.IOException;
038: import java.io.OutputStream;
039: import java.io.PrintWriter;
040: import java.util.Date;
041: import java.util.Map;
042: import java.util.Properties;
043: import java.util.HashMap;
044: import java.net.URISyntaxException;
045:
046: /**
047: * @version $Rev: 636945 $ $Date: 2008-03-13 18:21:27 -0700 $
048: */
049: public class Info2Properties {
050: private static Messages messages = new Messages(
051: Info2Properties.class);
052:
053: private static final String defaultServerUrl = "ejbd://localhost:4201";
054:
055: public static void main(String[] args) {
056:
057: CommandLineParser parser = new PosixParser();
058:
059: // create the Options
060: Options options = new Options();
061: options.addOption(option("v", "version",
062: "cmd.properties.opt.version"));
063: options
064: .addOption(option("h", "help",
065: "cmd.properties.opt.help"));
066: options.addOption(option("s", "server-url", "url",
067: "cmd.properties.opt.server"));
068:
069: CommandLine line = null;
070: try {
071: // parse the command line arguments
072: line = parser.parse(options, args);
073: } catch (ParseException exp) {
074: help(options);
075: System.exit(-1);
076: }
077:
078: if (line.hasOption("help")) {
079: help(options);
080: System.exit(0);
081: } else if (line.hasOption("version")) {
082: OpenEjbVersion.get().print(System.out);
083: System.exit(0);
084: }
085:
086: Properties p = new Properties();
087: p
088: .put(Context.INITIAL_CONTEXT_FACTORY,
089: "org.apache.openejb.client.RemoteInitialContextFactory");
090:
091: String serverUrl = defaultServerUrl;
092: if (line.hasOption(serverUrl)) {
093: serverUrl = line.getOptionValue("serverUrl");
094: }
095: p.put(Context.PROVIDER_URL, serverUrl);
096:
097: ConfigurationInfo configInfo = null;
098: try {
099: InitialContext ctx = new InitialContext(p);
100: configInfo = (ConfigurationInfo) ctx
101: .lookup("openejb/ConfigurationInfoBusinessRemote");
102: } catch (javax.naming.ServiceUnavailableException e) {
103: System.out.println(e.getCause().getMessage());
104: System.out.println(messages
105: .format("cmd.deploy.serverOffline"));
106: System.exit(1);
107: } catch (javax.naming.NamingException e) {
108: System.out
109: .println("ConfigurationInfo does not exist in server '"
110: + serverUrl
111: + "', check the server logs to ensure it exists and has not been removed.");
112: System.exit(2);
113: }
114:
115: File tempFile = null;
116: try {
117: tempFile = File.createTempFile("configrequest", "txt");
118: if (!tempFile.exists()) {
119: throw new IllegalStateException(
120: "Failed to create tmp file: "
121: + tempFile.getAbsolutePath());
122: }
123: } catch (Exception e) {
124: System.err.println("Temp file creation failed.");
125: e.printStackTrace();
126: System.exit(1);
127: }
128:
129: OpenEjbConfiguration configuration = null;
130: try {
131: configuration = configInfo
132: .getOpenEjbConfiguration(tempFile);
133: } catch (ConfigurationInfo.UnauthorizedException e) {
134: System.err
135: .println("This tool is currently crippled to only work with server's on the same physical machine. See this JIRA issue for details: http://issues.apache.org/jira/browse/OPENEJB-621");
136: System.exit(10);
137: }
138:
139: comment(i18n("cmd.properties.header"));
140: comment("");
141: comment("");
142: comment("Generated by OpenEJB "
143: + OpenEjbVersion.get().getVersion());
144: comment("On " + new Date().toString());
145: comment("");
146: comment("");
147: println("");
148: println("");
149:
150: comment("-------------------------------------------------");
151: comment(" Components configurable via openejb.xml");
152: comment("-------------------------------------------------");
153: println("");
154: println("");
155:
156: for (ServiceInfo info : configuration.containerSystem.containers) {
157: print(info);
158: }
159:
160: for (ServiceInfo info : configuration.facilities.connectionManagers) {
161: print(info);
162: }
163:
164: for (ServiceInfo info : configuration.facilities.resources) {
165: print(info);
166: }
167:
168: print(configuration.facilities.securityService);
169:
170: print(configuration.facilities.transactionService);
171:
172: println("");
173: comment("-------------------------------------------------");
174: comment(" Services configured via conf/<id>.properties");
175: comment("-------------------------------------------------");
176: println("");
177: println("");
178:
179: for (ServiceInfo info : configuration.facilities.services) {
180: print(info);
181: }
182:
183: println("");
184: comment("-------------------------------------------------");
185: comment(" Misc OpenEJB flags and properties");
186: comment("-------------------------------------------------");
187: println("");
188: printSystemProperties();
189:
190: }
191:
192: private static void printSystemProperties() {
193:
194: try {
195: Properties p = new Properties();
196: copyOpenEjbProperties(System.getProperties(), p);
197: copyOpenEjbProperties(SystemInstance.get().getProperties(),
198: p);
199: p.store(new Filter(System.out), null);
200:
201: p = System.getProperties();
202: String[] misc = { "os.version", "os.name", "os.arch",
203: "java.version", "java.vendor" };
204: for (String prop : misc) {
205: comment(prop + "=" + p.get(prop));
206: }
207: } catch (IOException e) {
208: e.printStackTrace(new PrintWriter(new CommentsFilter(
209: System.out)));
210: }
211: }
212:
213: private static void copyOpenEjbProperties(Properties source,
214: Properties dest) {
215: for (Map.Entry<Object, Object> entry : source.entrySet()) {
216: if (!(entry.getKey() instanceof String))
217: continue;
218: if (!(entry.getValue() instanceof String))
219: continue;
220:
221: String key = (String) entry.getKey();
222: if (key.startsWith("openejb.")) {
223: dest.put(entry.getKey(), entry.getValue());
224: }
225: }
226: }
227:
228: private static void comment(String text) {
229: text = text.replaceAll("\n", "\n# ");
230: print("# ");
231: println(text);
232: }
233:
234: private static void print(String text) {
235: System.out.print(text);
236: }
237:
238: private static void println(String text) {
239: System.out.println(text);
240: }
241:
242: private static void print(ServiceInfo info) {
243: try {
244:
245: println("");
246:
247: comment(info.service + "(id=" + info.id + ")");
248: comment("className: " + info.className);
249: // TODO: the codebase value usually isn't filled in, we should do that.
250: // comment("codebase: " + info.codebase);
251: comment("");
252: Properties p = new SuperProperties();
253:
254: String uri = "new://" + info.service;
255: if (info.service.matches("Container|Resource|Connector")) {
256: try {
257: Map query = new HashMap();
258: query.put("type", info.types.get(0));
259: uri += "?" + URISupport.createQueryString(query);
260: } catch (Exception e) {
261: }
262: }
263:
264: p.put(info.id, uri);
265:
266: for (Map.Entry<Object, Object> entry : info.properties
267: .entrySet()) {
268: if (!(entry.getKey() instanceof String))
269: continue;
270: if (!(entry.getValue() instanceof String))
271: continue;
272:
273: // If property name is 'password' replace value with 'xxxx' to protect it
274: if ("password"
275: .equalsIgnoreCase((String) entry.getKey())) {
276: p.put(info.id + "." + entry.getKey(), "xxxx");
277: } else {
278: p.put(info.id + "." + entry.getKey(), entry
279: .getValue());
280: }
281: }
282: p.store(new Filter(System.out), null);
283:
284: } catch (IOException e) {
285: System.out.println("# Printing service(id=" + info.id
286: + ") failed.");
287: e.printStackTrace(new PrintWriter(new CommentsFilter(
288: System.out)));
289: }
290:
291: }
292:
293: // Filter out the stupid date comment the Properties.store() method
294: // adds seemingly no matter what.
295: static class Filter extends java.io.FilterOutputStream {
296: private boolean pastFirstLine;
297:
298: public Filter(OutputStream out) {
299: super (out);
300: }
301:
302: public void write(int b) throws IOException {
303: if (pastFirstLine)
304: super .write(b);
305: else
306: pastFirstLine = b == '\n';
307: }
308:
309: }
310:
311: static class CommentsFilter extends java.io.FilterOutputStream {
312:
313: public CommentsFilter(OutputStream out) {
314: super (out);
315: }
316:
317: public void write(int b) throws IOException {
318: super .write(b);
319:
320: if (b == '\n')
321: super .write('#');
322: }
323:
324: }
325:
326: private static void help(Options options) {
327: HelpFormatter formatter = new HelpFormatter();
328: formatter.printHelp("properties [options]", "\n"
329: + i18n("cmd.properties.description"), options, "\n");
330: }
331:
332: private static Option option(String shortOpt, String longOpt,
333: String description) {
334: return OptionBuilder.withLongOpt(longOpt).withDescription(
335: i18n(description)).create(shortOpt);
336: }
337:
338: private static Option option(String shortOpt, String longOpt,
339: String argName, String description) {
340: return OptionBuilder.withLongOpt(longOpt).withArgName(argName)
341: .hasArg().withDescription(i18n(description)).create(
342: shortOpt);
343: }
344:
345: private static String i18n(String key) {
346: return messages.format(key);
347: }
348:
349: }
|