001 /** 002 * 003 * Copyright 2003-2004 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * 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 */ 017 package org.apache.geronimo.deployment.plugin.jmx; 018 019 import java.io.File; 020 import java.io.IOException; 021 import java.io.InputStream; 022 import java.net.InetAddress; 023 import java.net.NetworkInterface; 024 import java.net.URL; 025 import java.util.Enumeration; 026 import java.util.Iterator; 027 import java.util.Set; 028 import java.util.Map; 029 import java.util.List; 030 import java.util.ArrayList; 031 import java.util.Arrays; 032 import javax.enterprise.deploy.shared.CommandType; 033 import javax.enterprise.deploy.spi.Target; 034 import javax.enterprise.deploy.spi.TargetModuleID; 035 import javax.enterprise.deploy.spi.status.ProgressEvent; 036 import javax.enterprise.deploy.spi.status.ProgressListener; 037 import javax.management.MBeanServerConnection; 038 import javax.management.remote.JMXConnector; 039 import javax.security.auth.login.FailedLoginException; 040 041 import org.apache.geronimo.deployment.plugin.GeronimoDeploymentManager; 042 import org.apache.geronimo.deployment.plugin.local.AbstractDeployCommand; 043 import org.apache.geronimo.deployment.plugin.local.DistributeCommand; 044 import org.apache.geronimo.deployment.plugin.local.RedeployCommand; 045 import org.apache.geronimo.deployment.plugin.remote.RemoteDeployUtil; 046 import org.apache.geronimo.gbean.AbstractName; 047 import org.apache.geronimo.gbean.AbstractNameQuery; 048 import org.apache.geronimo.system.jmx.KernelDelegate; 049 import org.apache.geronimo.system.plugin.DownloadResults; 050 import org.apache.geronimo.system.plugin.PluginList; 051 import org.apache.geronimo.system.plugin.DownloadPoller; 052 import org.apache.geronimo.system.plugin.PluginMetadata; 053 import org.apache.geronimo.system.plugin.PluginInstaller; 054 import org.apache.geronimo.system.plugin.PluginRepositoryList; 055 import org.apache.geronimo.kernel.repository.Artifact; 056 import org.apache.commons.logging.Log; 057 import org.apache.commons.logging.LogFactory; 058 059 /** 060 * Connects to a Kernel in a remote VM (may or many not be on the same machine). 061 * 062 * @version $Rev: 437623 $ $Date: 2006-08-28 02:48:23 -0700 (Mon, 28 Aug 2006) $ 063 */ 064 public class RemoteDeploymentManager extends JMXDeploymentManager implements GeronimoDeploymentManager { 065 private static final Log log = LogFactory.getLog(RemoteDeploymentManager.class); 066 067 private JMXConnector jmxConnector; 068 private boolean isSameMachine; 069 070 public RemoteDeploymentManager(JMXConnector jmxConnector, String hostname) throws IOException { 071 this.jmxConnector = jmxConnector; 072 MBeanServerConnection mbServerConnection = jmxConnector.getMBeanServerConnection(); 073 initialize(new KernelDelegate(mbServerConnection)); 074 checkSameMachine(hostname); 075 } 076 077 public boolean isSameMachine() { 078 return isSameMachine; 079 } 080 081 private void checkSameMachine(String hostname) { 082 isSameMachine = false; 083 if(hostname.equals("localhost") || hostname.equals("127.0.0.1")) { 084 isSameMachine = true; 085 return; 086 } 087 try { 088 InetAddress dest = InetAddress.getByName(hostname); 089 Enumeration en = NetworkInterface.getNetworkInterfaces(); 090 while(en.hasMoreElements()) { 091 NetworkInterface iface = (NetworkInterface) en.nextElement(); 092 Enumeration ine = iface.getInetAddresses(); 093 while (ine.hasMoreElements()) { 094 InetAddress address = (InetAddress) ine.nextElement(); 095 if(address.equals(dest)) { 096 isSameMachine = true; 097 } 098 } 099 } 100 } catch (Exception e) { 101 log.error("Unable to look up host name '"+hostname+"'; assuming it is a different machine, but this may not get very far.", e); 102 } 103 } 104 105 public void release() { 106 super.release(); 107 try { 108 jmxConnector.close(); 109 jmxConnector = null; 110 } catch (IOException e) { 111 throw (IllegalStateException) new IllegalStateException("Unable to close connection").initCause(e); 112 } 113 } 114 115 protected DistributeCommand createDistributeCommand(Target[] targetList, File moduleArchive, File deploymentPlan) { 116 if(isSameMachine) { 117 return super.createDistributeCommand(targetList, moduleArchive, deploymentPlan); 118 } else { 119 return new org.apache.geronimo.deployment.plugin.remote.DistributeCommand(kernel, targetList, moduleArchive, deploymentPlan); 120 } 121 } 122 123 protected DistributeCommand createDistributeCommand(Target[] targetList, InputStream moduleArchive, InputStream deploymentPlan) { 124 if(isSameMachine) { 125 return super.createDistributeCommand(targetList, moduleArchive, deploymentPlan); 126 } else { 127 return new org.apache.geronimo.deployment.plugin.remote.DistributeCommand(kernel, targetList, moduleArchive, deploymentPlan); 128 } 129 } 130 131 protected RedeployCommand createRedeployCommand(TargetModuleID[] moduleIDList, File moduleArchive, File deploymentPlan) { 132 if(isSameMachine) { 133 return super.createRedeployCommand(moduleIDList, moduleArchive, deploymentPlan); 134 } else { 135 return new org.apache.geronimo.deployment.plugin.remote.RedeployCommand(kernel, moduleIDList, moduleArchive, deploymentPlan); 136 } 137 } 138 139 protected RedeployCommand createRedeployCommand(TargetModuleID[] moduleIDList, InputStream moduleArchive, InputStream deploymentPlan) { 140 if(isSameMachine) { 141 return super.createRedeployCommand(moduleIDList, moduleArchive, deploymentPlan); 142 } else { 143 return new org.apache.geronimo.deployment.plugin.remote.RedeployCommand(kernel, moduleIDList, moduleArchive, deploymentPlan); 144 } 145 } 146 147 public PluginList listPlugins(URL mavenRepository, String username, String password) throws FailedLoginException, IOException { 148 Set set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName())); 149 for (Iterator it = set.iterator(); it.hasNext();) { 150 AbstractName name = (AbstractName) it.next(); 151 PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class); 152 PluginList results = installer.listPlugins(mavenRepository, username, password); 153 kernel.getProxyManager().destroyProxy(installer); 154 return results; 155 } 156 return null; 157 } 158 159 public DownloadResults install(PluginList installList, String username, String password) { 160 Set set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName())); 161 for (Iterator it = set.iterator(); it.hasNext();) { 162 AbstractName name = (AbstractName) it.next(); 163 PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class); 164 DownloadResults results = installer.install(installList, username, password); 165 kernel.getProxyManager().destroyProxy(installer); 166 return results; 167 } 168 return null; 169 } 170 171 public void install(PluginList configsToInstall, String username, String password, DownloadPoller poller) { 172 Set set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName())); 173 for (Iterator it = set.iterator(); it.hasNext();) { 174 AbstractName name = (AbstractName) it.next(); 175 PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class); 176 installer.install(configsToInstall, username, password, poller); 177 kernel.getProxyManager().destroyProxy(installer); 178 return; 179 } 180 } 181 182 public Object startInstall(PluginList configsToInstall, String username, String password) { 183 Set set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName())); 184 for (Iterator it = set.iterator(); it.hasNext();) { 185 AbstractName name = (AbstractName) it.next(); 186 PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class); 187 Object result = installer.startInstall(configsToInstall, username, password); 188 kernel.getProxyManager().destroyProxy(installer); 189 return result; 190 } 191 return null; 192 } 193 194 public Object startInstall(File carFile, String username, String password) { 195 File[] args = new File[]{carFile}; 196 if(!isSameMachine) { 197 AbstractDeployCommand progress = new AbstractDeployCommand(CommandType.DISTRIBUTE, kernel, null, null, null, null, false) { 198 public void run() { 199 } 200 }; 201 progress.addProgressListener(new ProgressListener() { 202 public void handleProgressEvent(ProgressEvent event) { 203 log.info(event.getDeploymentStatus().getMessage()); 204 } 205 }); 206 RemoteDeployUtil.uploadFilesToServer(args, progress); 207 } 208 Set set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName())); 209 for (Iterator it = set.iterator(); it.hasNext();) { 210 AbstractName name = (AbstractName) it.next(); 211 PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class); 212 Object result = installer.startInstall(carFile, username, password); 213 kernel.getProxyManager().destroyProxy(installer); 214 return result; 215 } 216 return null; 217 } 218 219 public DownloadResults checkOnInstall(Object key) { 220 Set set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName())); 221 for (Iterator it = set.iterator(); it.hasNext();) { 222 AbstractName name = (AbstractName) it.next(); 223 PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class); 224 DownloadResults result = installer.checkOnInstall(key); 225 kernel.getProxyManager().destroyProxy(installer); 226 return result; 227 } 228 return null; 229 } 230 231 public Map getInstalledPlugins() { 232 Set set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName())); 233 for (Iterator it = set.iterator(); it.hasNext();) { 234 AbstractName name = (AbstractName) it.next(); 235 PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class); 236 Map result = installer.getInstalledPlugins(); 237 kernel.getProxyManager().destroyProxy(installer); 238 return result; 239 } 240 return null; 241 } 242 243 public PluginMetadata getPluginMetadata(Artifact configId) { 244 Set set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName())); 245 for (Iterator it = set.iterator(); it.hasNext();) { 246 AbstractName name = (AbstractName) it.next(); 247 PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class); 248 PluginMetadata result = installer.getPluginMetadata(configId); 249 kernel.getProxyManager().destroyProxy(installer); 250 return result; 251 } 252 return null; 253 } 254 255 public void updatePluginMetadata(PluginMetadata metadata) { 256 Set set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName())); 257 for (Iterator it = set.iterator(); it.hasNext();) { 258 AbstractName name = (AbstractName) it.next(); 259 PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class); 260 installer.updatePluginMetadata(metadata); 261 kernel.getProxyManager().destroyProxy(installer); 262 return; 263 } 264 } 265 266 public URL[] getRepositories() { 267 List list = new ArrayList(); 268 Set set = kernel.listGBeans(new AbstractNameQuery(PluginRepositoryList.class.getName())); 269 for (Iterator it = set.iterator(); it.hasNext();) { 270 AbstractName name = (AbstractName) it.next(); 271 PluginRepositoryList repo = (PluginRepositoryList) kernel.getProxyManager().createProxy(name, PluginRepositoryList.class); 272 list.addAll(Arrays.asList(repo.getRepositories())); 273 kernel.getProxyManager().destroyProxy(repo); 274 } 275 return (URL[]) list.toArray(new URL[list.size()]); 276 } 277 }