1 23 24 package com.sun.enterprise.cli.commands; 25 26 import java.io.File ; 27 import java.lang.reflect.Method ; 28 import java.io.PrintStream ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import com.sun.enterprise.util.i18n.StringManager; 32 import com.sun.enterprise.cli.framework.CommandException; 33 34 45 public final class DerbyControl 46 { 47 final public static String DB_LOG_FILENAME = "derby.log"; 48 final private String derbyCommand; 49 final private String derbyHost; 50 final private String derbyPort; 51 final private String derbyHome; 52 final private boolean redirect; 53 54 public DerbyControl(final String dc, final String dht, final String dp, 56 final String redirect, final String dhe) 57 { 58 this.derbyCommand = dc; 59 this.derbyHost = dht; 60 this.derbyPort = dp; 61 this.derbyHome = dhe; 62 this.redirect = new Boolean (redirect).booleanValue(); 63 if (this.redirect) { 64 65 try { 66 String dbLog = ""; 67 if (this.derbyHome == null) { 68 dbLog = createTempLogFile(); 71 } 72 else { 73 dbLog = createDBLog(this.derbyHome); 74 } 75 76 PrintStream printStream = new PrintStream (new FileOutputStream (dbLog, true), true); 78 System.setOut(printStream); 79 System.setErr(printStream); 80 } 81 catch (Throwable t) { 82 t.printStackTrace(); 83 System.exit(2); 85 } 86 } 87 if (this.derbyHome!=null && this.derbyHome.length()>0) { 89 System.setProperty("derby.system.home", this.derbyHome); 90 } 91 System.setProperty("derby.infolog.append", "true"); 93 } 94 95 public DerbyControl(final String dc, final String dht, final String dp) 97 { 98 this(dc,dht,dp,"true", null); 99 } 100 101 public DerbyControl(final String dc, final String dht, final String dp, final String redirect) 103 { 104 this(dc,dht,dp,redirect,null); 105 } 106 107 111 private void invokeNetworkServerControl() 112 { 113 try { 114 Class networkServer = Class.forName("org.apache.derby.drda.NetworkServerControl"); 115 Method networkServerMethod = networkServer.getDeclaredMethod("main", 116 new Class []{String [].class}); 117 Object [] paramObj = new Object []{new String []{derbyCommand, "-h", derbyHost, "-p", derbyPort}}; 118 119 networkServerMethod.invoke(networkServer, paramObj); 120 } 121 catch (Throwable t) { 122 t.printStackTrace(); 123 System.exit(2); 124 } 125 } 126 127 128 134 private String createDBLog(final String dbHome) throws Exception 135 { 136 final File fDBHome = new File (dbHome); 138 String dbLogFileName = ""; 139 140 final StringManager lsm = StringManager.getManager(DerbyControl.class); 141 if (fDBHome.isDirectory() && fDBHome.canWrite()) { 142 final File fDBLog = new File (dbHome, DB_LOG_FILENAME); 143 dbLogFileName = fDBLog.toString(); 144 145 if (fDBLog.exists() && !fDBLog.canWrite()) { 147 System.out.println(lsm.getString("UnableToAccessDatabaseLog", dbLogFileName)); 148 System.out.println(lsm.getString("ContinueStartingDatabase")); 149 dbLogFileName = createTempLogFile(); 152 } 153 else if (!fDBLog.exists()) { 154 fDBLog.createNewFile(); 156 } 157 } 158 else { 159 System.out.println(lsm.getString("InvalidDirectory", dbHome)); 160 System.out.println(lsm.getString("ContinueStartingDatabase")); 161 dbLogFileName = createTempLogFile(); 164 } 165 return dbLogFileName; 166 } 167 168 171 private String createTempLogFile() throws CommandException 172 { 173 String tempFileName = ""; 174 try { 175 final File fTemp = File.createTempFile("foo", null); 176 fTemp.deleteOnExit(); 177 tempFileName = fTemp.toString(); 178 } 179 catch (IOException ioe) { 180 final StringManager lsm = StringManager.getManager(DerbyControl.class); 181 throw new CommandException(lsm.getString("UnableToAccessDatabaseLog", tempFileName)); 182 } 183 return tempFileName; 184 } 185 186 public static void main(String [] args) { 187 188 if (args.length<3){ 189 System.out.println("paramters not specified."); 190 System.out.println("DerbyControl <derby command> <derby host> <derby port> <derby home> <redirect output>"); 191 System.exit(1); 192 } 193 194 DerbyControl derbyControl = null; 195 if (args.length == 3) 196 derbyControl = new DerbyControl(args[0], args[1], args[2]); 197 else if (args.length == 4 ) 198 derbyControl = new DerbyControl(args[0], args[1], args[2], args[3]); 199 else if (args.length > 4) 200 derbyControl = new DerbyControl(args[0], args[1], args[2], args[3], args[4]); 201 if (derbyControl != null) 202 derbyControl.invokeNetworkServerControl(); 203 } 204 } 205 206 207 208 | Popular Tags |