Home » openejb-3.1.2-src » org.apache » openejb » assembler » classic » cmd » [javadoc | source]

    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.openejb.assembler.classic.cmd;
   18   
   19   import java.io.File;
   20   import java.io.IOException;
   21   import java.io.OutputStream;
   22   import java.io.PrintWriter;
   23   import java.util.Date;
   24   import java.util.HashMap;
   25   import java.util.Map;
   26   import java.util.Properties;
   27   
   28   import javax.naming.Context;
   29   import javax.naming.InitialContext;
   30   
   31   import org.apache.commons.cli.CommandLine;
   32   import org.apache.commons.cli.CommandLineParser;
   33   import org.apache.commons.cli.HelpFormatter;
   34   import org.apache.commons.cli.Option;
   35   import org.apache.commons.cli.OptionBuilder;
   36   import org.apache.commons.cli.Options;
   37   import org.apache.commons.cli.ParseException;
   38   import org.apache.commons.cli.PosixParser;
   39   import org.apache.openejb.assembler.classic.OpenEjbConfiguration;
   40   import org.apache.openejb.assembler.classic.ServiceInfo;
   41   import org.apache.openejb.loader.SystemInstance;
   42   import org.apache.openejb.util.Messages;
   43   import org.apache.openejb.util.OpenEjbVersion;
   44   import org.apache.openejb.util.SuperProperties;
   45   import org.apache.openejb.util.URISupport;
   46   
   47   /**
   48    * @version $Rev: 747955 $ $Date: 2009-02-25 15:12:32 -0800 (Wed, 25 Feb 2009) $
   49    */
   50   public class Info2Properties {
   51       private static Messages messages = new Messages(Info2Properties.class);
   52   
   53       private static final String defaultServerUrl = "ejbd://localhost:4201";
   54   
   55       public static void main(String[] args) {
   56   
   57           CommandLineParser parser = new PosixParser();
   58   
   59           // create the Options
   60           Options options = new Options();
   61           options.addOption(option("v", "version", "cmd.properties.opt.version"));
   62           options.addOption(option("h", "help", "cmd.properties.opt.help"));
   63           options.addOption(option("s", "server-url", "url", "cmd.properties.opt.server"));
   64   
   65           CommandLine line = null;
   66           try {
   67               // parse the command line arguments
   68               line = parser.parse(options, args);
   69           } catch (ParseException exp) {
   70               help(options);
   71               System.exit(-1);
   72           }
   73   
   74           if (line.hasOption("help")) {
   75               help(options);
   76               System.exit(0);
   77           } else if (line.hasOption("version")) {
   78               OpenEjbVersion.get().print(System.out);
   79               System.exit(0);
   80           }
   81   
   82           Properties p = new Properties();
   83           p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
   84   
   85           String serverUrl = line.getOptionValue("server-url", defaultServerUrl);
   86           p.put(Context.PROVIDER_URL, serverUrl);
   87   
   88           ConfigurationInfo configInfo = null;
   89           try {
   90               InitialContext ctx = new InitialContext(p);
   91               configInfo = (ConfigurationInfo) ctx.lookup("openejb/ConfigurationInfoBusinessRemote");
   92           } catch (javax.naming.ServiceUnavailableException e) {
   93               System.out.println(e.getCause().getMessage());
   94               System.out.println(messages.format("cmd.deploy.serverOffline"));
   95               System.exit(1);
   96           } catch (javax.naming.NamingException e) {
   97               System.out.println("ConfigurationInfo does not exist in server '" + serverUrl + "', check the server logs to ensure it exists and has not been removed.");
   98               System.exit(2);
   99           }
  100   
  101           File tempFile = null;
  102           try {
  103               tempFile = File.createTempFile("configrequest", "txt");
  104               if (!tempFile.exists()) {
  105                   throw new IllegalStateException("Failed to create tmp file: " + tempFile.getAbsolutePath());
  106               }
  107           } catch (Exception e) {
  108               System.err.println("Temp file creation failed.");
  109               e.printStackTrace();
  110               System.exit(1);
  111           }
  112   
  113           OpenEjbConfiguration configuration = null;
  114           try {
  115               configuration = configInfo.getOpenEjbConfiguration(tempFile);
  116           } catch (ConfigurationInfo.UnauthorizedException e) {
  117               System.err.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");
  118               System.exit(10);
  119           }
  120   
  121           printConfig(configuration);
  122       }
  123   
  124       public static void printLocalConfig() {
  125           OpenEjbConfiguration configuration = SystemInstance.get().getComponent(OpenEjbConfiguration.class);
  126           if (configuration != null){
  127               printConfig(configuration);
  128           }
  129       }
  130   
  131       public static void printConfig(OpenEjbConfiguration configuration) {
  132           comment(i18n("cmd.properties.header"));
  133           comment("");
  134           comment("");
  135           comment("Generated by OpenEJB " + OpenEjbVersion.get().getVersion());
  136           comment("On " + new Date().toString());
  137           comment("");
  138           comment("");
  139           println("");
  140           println("");
  141   
  142           comment("-------------------------------------------------");
  143           comment(" Components configurable via openejb.xml");
  144           comment("-------------------------------------------------");
  145           println("");
  146           println("");
  147   
  148           for (ServiceInfo info : configuration.containerSystem.containers) {
  149               print(info);
  150           }
  151   
  152           for (ServiceInfo info : configuration.facilities.connectionManagers) {
  153               print(info);
  154           }
  155   
  156           for (ServiceInfo info : configuration.facilities.resources) {
  157               print(info);
  158           }
  159   
  160           print(configuration.facilities.securityService);
  161   
  162           print(configuration.facilities.transactionService);
  163   
  164           println("");
  165           comment("-------------------------------------------------");
  166           comment(" Services configured via conf/<id>.properties");
  167           comment("-------------------------------------------------");
  168           println("");
  169           println("");
  170   
  171           for (ServiceInfo info : configuration.facilities.services) {
  172               print(info);
  173           }
  174   
  175           println("");
  176           comment("-------------------------------------------------");
  177           comment(" Misc OpenEJB flags and properties");
  178           comment("-------------------------------------------------");
  179           println("");
  180           printSystemProperties();
  181       }
  182   
  183       private static void printSystemProperties() {
  184   
  185           try {
  186               SuperProperties p = new SuperProperties();
  187               p.setSpaceBetweenProperties(false);
  188               p.setKeyValueSeparator(" = ");
  189               copyOpenEjbProperties(System.getProperties(), p);
  190               copyOpenEjbProperties(SystemInstance.get().getProperties(), p);
  191               p.store(System.out, null);
  192   
  193   
  194               Properties p2 = System.getProperties();
  195               String[] misc = {"os.version", "os.name", "os.arch", "java.version", "java.vendor"};
  196               for (String prop : misc) {
  197                   comment(prop + "=" + p2.get(prop));
  198               }
  199           } catch (IOException e) {
  200               e.printStackTrace(new PrintWriter(new CommentsFilter(System.out)));
  201           }
  202       }
  203   
  204       private static void copyOpenEjbProperties(Properties source, Properties dest) {
  205           for (Map.Entry<Object, Object> entry : source.entrySet()) {
  206               if (!(entry.getKey() instanceof String)) continue;
  207               if (!(entry.getValue() instanceof String)) continue;
  208   
  209               String key = (String) entry.getKey();
  210               if (key.startsWith("openejb.")) {
  211                   dest.put(entry.getKey(), entry.getValue());
  212               }
  213           }
  214       }
  215   
  216       private static void comment(String text) {
  217           text = text.replaceAll("\n", "\n# ");
  218           print("# ");
  219           println(text);
  220       }
  221   
  222       private static void print(String text) {
  223           System.out.print(text);
  224       }
  225   
  226       private static void println(String text) {
  227           System.out.println(text);
  228       }
  229   
  230       private static void print(ServiceInfo info) {
  231           try {
  232   
  233               println("");
  234   
  235               comment(info.service + "(id=" + info.id + ")");
  236               comment("className: " + info.className);
  237               // TODO: the codebase value usually isn't filled in, we should do that.
  238               // comment("codebase: " + info.codebase);
  239               comment("");
  240               SuperProperties p = new SuperProperties();
  241               p.setSpaceBetweenProperties(false);
  242               p.setKeyValueSeparator(" = ");
  243   
  244   
  245               String uri = "new://" + info.service;
  246               if (info.service.matches("Container|Resource|Connector")){
  247                   try {
  248                       Map query = new HashMap();
  249                       query.put("type", info.types.get(0));
  250                       uri += "?" + URISupport.createQueryString(query);
  251                   } catch (Exception e) {
  252                   }
  253               }
  254   
  255               p.put(info.id, uri);
  256               
  257               for (Map.Entry<Object, Object> entry : info.properties.entrySet()) {
  258                   if (!(entry.getKey() instanceof String)) continue;
  259                   if (!(entry.getValue() instanceof String)) continue;
  260   
  261                   // If property name is 'password' replace value with 'xxxx' to protect it
  262                   if ("password".equalsIgnoreCase((String) entry.getKey())) {
  263                       p.put(info.id + "." + entry.getKey(), "xxxx");
  264                   } else {
  265                       p.put(info.id + "." + entry.getKey(), entry.getValue());
  266                   }
  267               }
  268               p.store(System.out, null);
  269   
  270           } catch (IOException e) {
  271               System.out.println("# Printing service(id=" + info.id + ") failed.");
  272               e.printStackTrace(new PrintWriter(new CommentsFilter(System.out)));
  273           }
  274   
  275       }
  276   
  277       // Filter out the stupid date comment the Properties.store() method
  278       // adds seemingly no matter what.
  279       static class Filter extends java.io.FilterOutputStream {
  280           private boolean pastFirstLine;
  281   
  282           public Filter(OutputStream out) {
  283               super(out);
  284           }
  285   
  286           public void write(int b) throws IOException {
  287               if (pastFirstLine) super.write(b);
  288               else pastFirstLine = b == '\n';
  289           }
  290   
  291       }
  292   
  293       static class CommentsFilter extends java.io.FilterOutputStream {
  294   
  295           public CommentsFilter(OutputStream out) {
  296               super(out);
  297           }
  298   
  299           public void write(int b) throws IOException {
  300               super.write(b);
  301   
  302               if (b == '\n') super.write('#');
  303           }
  304   
  305       }
  306   
  307       private static void help(Options options) {
  308           HelpFormatter formatter = new HelpFormatter();
  309           formatter.printHelp("properties [options]", "\n" + i18n("cmd.properties.description"), options, "\n");
  310       }
  311   
  312       private static Option option(String shortOpt, String longOpt, String description) {
  313           return OptionBuilder.withLongOpt(longOpt).withDescription(i18n(description)).create(shortOpt);
  314       }
  315   
  316       private static Option option(String shortOpt, String longOpt, String argName, String description) {
  317           return OptionBuilder.withLongOpt(longOpt).withArgName(argName).hasArg().withDescription(i18n(description)).create(shortOpt);
  318       }
  319   
  320       private static String i18n(String key) {
  321           return messages.format(key);
  322       }
  323   
  324   }

Home » openejb-3.1.2-src » org.apache » openejb » assembler » classic » cmd » [javadoc | source]