| 1 |  |  package com.aurea.maven.plugins.sonic.utils; | 
  | 2 |  |   | 
  | 3 |  |  import java.io.BufferedReader; | 
  | 4 |  |  import java.io.File; | 
  | 5 |  |  import java.io.InputStreamReader; | 
  | 6 |  |  import java.util.Hashtable; | 
  | 7 |  |   | 
  | 8 |  |  import javax.management.ObjectName; | 
  | 9 |  |   | 
  | 10 |  |  import com.sonicsw.mf.jmx.client.JMSConnectorAddress; | 
  | 11 |  |  import com.sonicsw.mf.jmx.client.JMSConnectorClient; | 
  | 12 |  |  import com.sonicsw.mf.mgmtapi.runtime.IAgentProxy; | 
  | 13 |  |  import com.sonicsw.mf.mgmtapi.runtime.IDirectoryServiceProxy; | 
  | 14 |  |  import com.sonicsw.mf.mgmtapi.runtime.MFProxyFactory; | 
  | 15 |  |  import com.sonicsw.mf.mgmtapi.runtime.ProxyRuntimeException; | 
  | 16 |  |  import com.sonicsw.mq.mgmtapi.runtime.IBrokerProxy; | 
  | 17 |  |  import com.sonicsw.mq.mgmtapi.runtime.MQProxyFactory; | 
  | 18 |  |   | 
  | 19 |  |   | 
  | 20 |  |   | 
  | 21 |  |   | 
  | 22 |  |  public class SonicInvoker extends JavaInvoker { | 
  | 23 |  |     | 
  | 24 |  |   | 
  | 25 |  |     | 
  | 26 |  |   | 
  | 27 |  |   | 
  | 28 |  |   | 
  | 29 |  |   | 
  | 30 |  |   | 
  | 31 |  |    public SonicInvoker(final String _sonicDirectory) throws Exception { | 
  | 32 |  |   | 
  | 33 | 0 |      super("com.sonicsw.mf.framework.agent.ci.LaunchContainer"); | 
  | 34 | 0 |      setWorkingDir(_sonicDirectory); | 
  | 35 | 0 |      configureSonicRuntime(); | 
  | 36 | 0 |    } | 
  | 37 |  |     | 
  | 38 |  |     | 
  | 39 |  |   | 
  | 40 |  |   | 
  | 41 |  |   | 
  | 42 |  |    public final void configureSonicRuntime() throws Exception { | 
  | 43 |  |   | 
  | 44 | 0 |      File sonicDir = new File(getWorkdir()); | 
  | 45 | 0 |      checkDir(sonicDir); | 
  | 46 | 0 |      File sonicLib = new File(sonicDir, "lib"); | 
  | 47 | 0 |      checkDir(sonicLib); | 
  | 48 |  |       | 
  | 49 | 0 |      addToBootClassPath(new File(sonicLib, "jmxri.jar").getAbsolutePath()); | 
  | 50 |  |       | 
  | 51 | 0 |      addToClassPath(new File(sonicLib, "MFLaunch.jar").getAbsolutePath()); | 
  | 52 | 0 |      addToClassPath(new File(sonicLib, "rsa_ssl.jar").getAbsolutePath()); | 
  | 53 |  |       | 
  | 54 | 0 |      setProperty("java.protocol.handler.pkgs", "progress.message.net"); | 
  | 55 | 0 |      setProperty("SSL_CA_CERTIFICATES_DIR", new File(sonicDir, "certs/CA").getAbsolutePath()); | 
  | 56 | 0 |      setProperty("sonicsw.home", getWorkdir()); | 
  | 57 | 0 |      setProperty("sonicsw.mf.allowRestart", "true"); | 
  | 58 |  |       | 
  | 59 | 0 |      addArgument("container.xml"); | 
  | 60 | 0 |      addArgument("ds.xml"); | 
  | 61 | 0 |    } | 
  | 62 |  |     | 
  | 63 |  |    private void checkDir(final File _dir) throws Exception { | 
  | 64 |  |   | 
  | 65 | 0 |      if (!_dir.exists()) { | 
  | 66 | 0 |        throw new Exception("Directory " + _dir.getAbsolutePath() + " does not exist."); | 
  | 67 |  |      } | 
  | 68 |  |       | 
  | 69 | 0 |      if (!_dir.isDirectory()) { | 
  | 70 | 0 |        throw new Exception("Directory " + _dir.getAbsolutePath() + " is not a directory."); | 
  | 71 |  |      } | 
  | 72 | 0 |    } | 
  | 73 |  |     | 
  | 74 |  |     | 
  | 75 |  |   | 
  | 76 |  |   | 
  | 77 |  |   | 
  | 78 |  |    @SuppressWarnings("deprecation") | 
  | 79 |  |  public static void main(final String[] _args) { | 
  | 80 |  |   | 
  | 81 |  |      try { | 
  | 82 | 0 |        System.out.println("Invoking"); | 
  | 83 |  |         | 
  | 84 |  |         | 
  | 85 |  |         | 
  | 86 | 0 |        ProcessBuilder process = new ProcessBuilder("C:\\Software\\Sonic7.6\\TestImage\\SonicData\\startCtManagement.bat"); | 
  | 87 | 0 |        process.directory(new File("C:\\Software\\Sonic7.6\\TestImage\\SonicData")); | 
  | 88 | 0 |        Process p = process.start(); | 
  | 89 | 0 |        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); | 
  | 90 |  |        String input; | 
  | 91 | 0 |        while (!(input = br.readLine()).toUpperCase().contains("STARTUP COMPLETE")) { | 
  | 92 | 0 |          System.out.println("Sonic: " + input); | 
  | 93 |  |        } | 
  | 94 |  |         | 
  | 95 | 0 |        System.out.println("Found: " + input + "\nContinuing"); | 
  | 96 |  |         | 
  | 97 | 0 |        System.out.println("Done Invoking"); | 
  | 98 | 0 |        br.close(); | 
  | 99 | 0 |        System.out.println("Sleeping"); | 
  | 100 | 0 |        Thread.sleep(10000); | 
  | 101 |  |         | 
  | 102 |  |         | 
  | 103 | 0 |        System.out.println("Performing the configuration"); | 
  | 104 | 0 |        OffLineDSConfigurator ods = new OffLineDSConfigurator(); | 
  | 105 | 0 |        ods.go(); | 
  | 106 |  |         | 
  | 107 | 0 |        System.out.println("Sleeping for shutdown"); | 
  | 108 | 0 |        Thread.sleep(10000); | 
  | 109 |  |         | 
  | 110 | 0 |        System.out.println("Shutting Down"); | 
  | 111 |  |         | 
  | 112 |  |   | 
  | 113 |  |   | 
  | 114 | 0 |        JMSConnectorClient connector = null; | 
  | 115 |  |         | 
  | 116 |  |        try { | 
  | 117 |  |           | 
  | 118 | 0 |          System.out.println("Get Connector"); | 
  | 119 | 0 |          connector = getConnector("tcp://localhost:3506", "Administrator", "Administrator"); | 
  | 120 |  |           | 
  | 121 | 0 |          System.out.println("Agent Proxy"); | 
  | 122 | 0 |          IAgentProxy agent = getAgentProxy(connector, "Domain1", "ctManagement"); | 
  | 123 |  |           | 
  | 124 | 0 |          System.out.println("shutdown"); | 
  | 125 | 0 |          agent.shutdown(); | 
  | 126 | 0 |          System.out.println("Shutdown Done"); | 
  | 127 | 0 |        } catch (Throwable t) { | 
  | 128 | 0 |          t.printStackTrace(); | 
  | 129 | 0 |          if (t instanceof ProxyRuntimeException) { | 
  | 130 | 0 |            System.err.println("Caused by:"); | 
  | 131 | 0 |            ((ProxyRuntimeException) t).getTargetException().printStackTrace(); | 
  | 132 |  |          } | 
  | 133 |  |        } finally { | 
  | 134 | 0 |          if (connector != null) { | 
  | 135 | 0 |            connector.disconnect(); | 
  | 136 |  |          } | 
  | 137 |  |        } | 
  | 138 | 0 |      } catch (Exception e) { | 
  | 139 | 0 |        e.printStackTrace(); | 
  | 140 | 0 |      } | 
  | 141 | 0 |    } | 
  | 142 |  |     | 
  | 143 |  |     | 
  | 144 |  |   | 
  | 145 |  |   | 
  | 146 |  |   | 
  | 147 |  |   | 
  | 148 |  |   | 
  | 149 |  |   | 
  | 150 |  |   | 
  | 151 |  |   | 
  | 152 |  |   | 
  | 153 |  |   | 
  | 154 |  |   | 
  | 155 |  |   | 
  | 156 |  |   | 
  | 157 |  |   | 
  | 158 |  |    public static JMSConnectorClient getConnector(final String _urls, final String _user, final String _pwd) | 
  | 159 |  |      throws Exception { | 
  | 160 |  |   | 
  | 161 | 0 |      Hashtable<String, String> env = new Hashtable<String, String>(); | 
  | 162 | 0 |      env.put("ConnectionURLs", _urls); | 
  | 163 | 0 |      env.put("DefaultUser", _user); | 
  | 164 | 0 |      env.put("DefaultPassword", _pwd); | 
  | 165 |  |       | 
  | 166 | 0 |      JMSConnectorAddress address = new JMSConnectorAddress(env); | 
  | 167 | 0 |      JMSConnectorClient connector = new JMSConnectorClient(); | 
  | 168 | 0 |      connector.connect(address, 10000); | 
  | 169 |  |       | 
  | 170 | 0 |      return connector; | 
  | 171 |  |    } | 
  | 172 |  |     | 
  | 173 |  |     | 
  | 174 |  |   | 
  | 175 |  |   | 
  | 176 |  |   | 
  | 177 |  |   | 
  | 178 |  |   | 
  | 179 |  |   | 
  | 180 |  |   | 
  | 181 |  |   | 
  | 182 |  |   | 
  | 183 |  |   | 
  | 184 |  |   | 
  | 185 |  |   | 
  | 186 |  |    public static IAgentProxy getAgentProxy(final JMSConnectorClient _connector, final String _domain, | 
  | 187 |  |      final String _container) throws Exception { | 
  | 188 |  |   | 
  | 189 | 0 |      ObjectName jmxName = new ObjectName(_domain + "." + _container + ":ID=AGENT"); | 
  | 190 | 0 |      return MFProxyFactory.createAgentProxy(_connector, jmxName); | 
  | 191 |  |    } | 
  | 192 |  |     | 
  | 193 |  |     | 
  | 194 |  |   | 
  | 195 |  |   | 
  | 196 |  |   | 
  | 197 |  |   | 
  | 198 |  |   | 
  | 199 |  |   | 
  | 200 |  |   | 
  | 201 |  |   | 
  | 202 |  |   | 
  | 203 |  |   | 
  | 204 |  |   | 
  | 205 |  |    public static IDirectoryServiceProxy getDirectoryServiceProxy(final JMSConnectorClient _connector, | 
  | 206 |  |      final String _domain) throws Exception { | 
  | 207 |  |   | 
  | 208 | 0 |      ObjectName jmxName = new ObjectName(_domain + ".DIRECTORY SERVICE:ID=DIRECTORY SERVICE"); | 
  | 209 | 0 |      return MFProxyFactory.createDirectoryServiceProxy(_connector, jmxName); | 
  | 210 |  |    } | 
  | 211 |  |     | 
  | 212 |  |     | 
  | 213 |  |   | 
  | 214 |  |   | 
  | 215 |  |   | 
  | 216 |  |   | 
  | 217 |  |   | 
  | 218 |  |   | 
  | 219 |  |   | 
  | 220 |  |   | 
  | 221 |  |   | 
  | 222 |  |   | 
  | 223 |  |   | 
  | 224 |  |   | 
  | 225 |  |   | 
  | 226 |  |   | 
  | 227 |  |    public static IBrokerProxy getBrokerProxy(final JMSConnectorClient _connector, final String _domain, | 
  | 228 |  |      final String _container, final String _id) throws Exception { | 
  | 229 |  |   | 
  | 230 | 0 |      ObjectName jmxName = new ObjectName(_domain + "." + _container + ":ID=" + _id); | 
  | 231 | 0 |      return MQProxyFactory.createBrokerProxy(_connector, jmxName); | 
  | 232 |  |    } | 
  | 233 |  |  } |