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.geronimo.st.v11.core; 18 19 import java.net.URL; 20 import java.util.List; 21 import java.util.Set; 22 23 import javax.management.MBeanServerConnection; 24 import javax.naming.directory.NoSuchAttributeException; 25 26 import org.apache.geronimo.gbean.AbstractName; 27 import org.apache.geronimo.gbean.AbstractNameQuery; 28 import org.apache.geronimo.gbean.GBeanData; 29 import org.apache.geronimo.kernel.GBeanNotFoundException; 30 import org.apache.geronimo.kernel.Kernel; 31 import org.apache.geronimo.kernel.config.Configuration; 32 import org.apache.geronimo.kernel.config.PersistentConfigurationList; 33 import org.apache.geronimo.kernel.repository.Artifact; 34 import org.apache.geronimo.st.core.Activator; 35 import org.apache.geronimo.st.core.GeronimoServerBehaviourDelegate; 36 import org.apache.geronimo.st.v11.core.internal.DependencyHelper; 37 import org.apache.geronimo.st.v11.core.internal.Trace; 38 import org.apache.geronimo.system.jmx.KernelDelegate; 39 import org.eclipse.core.runtime.IPath; 40 import org.eclipse.core.runtime.Status; 41 import org.eclipse.wst.server.core.IModule; 42 import org.eclipse.wst.server.core.IServer; 43 import org.eclipse.wst.server.core.internal.IModulePublishHelper; 44 45 /** 46 * @version $Rev: 509683 $ $Date: 2007-02-20 12:47:35 -0500 (Tue, 20 Feb 2007) $ 47 */ 48 public class GeronimoServerBehaviour extends GeronimoServerBehaviourDelegate implements IModulePublishHelper { 49 50 private Kernel kernel = null; 51 52 public GeronimoServerBehaviour() { 53 super(); 54 } 55 56 /* (non-Javadoc) 57 * @see org.apache.geronimo.st.core.GeronimoServerBehaviourDelegate#stopKernel() 58 */ 59 protected void stopKernel() { 60 if (kernel != null) { 61 kernel.shutdown(); 62 kernel = null; 63 } 64 } 65 66 /** 67 * @return 68 * @throws SecurityException 69 */ 70 protected Kernel getKernel() throws SecurityException { 71 if (kernel == null) { 72 try { 73 MBeanServerConnection connection = getServerConnection(); 74 if (connection != null) 75 kernel = new KernelDelegate(connection); 76 } catch (SecurityException e) { 77 throw e; 78 } catch (Exception e) { 79 Trace.trace(Trace.WARNING, "Kernel connection failed. " 80 + e.getMessage()); 81 } 82 } 83 return kernel; 84 } 85 86 /* 87 * (non-Javadoc) 88 * 89 * @see org.apache.geronimo.st.core.IGeronimoServerBehavior#isKernelAlive() 90 */ 91 public boolean isKernelAlive() { 92 try { 93 return getKernel() != null && kernel.isRunning(); 94 } catch (SecurityException e) { 95 Activator.log(Status.ERROR, "Invalid username and/or password.", e); 96 pingThread.interrupt(); 97 if (getServer().getServerState() != IServer.STATE_STOPPED) { 98 stop(true); 99 } 100 } catch (Exception e) { 101 Activator.log(Status.WARNING, "Geronimo Server may have been terminated manually outside of workspace.", e); 102 kernel = null; 103 } 104 return false; 105 } 106 107 /* 108 * (non-Javadoc) 109 * 110 * @see org.apache.geronimo.st.core.IGeronimoServerBehavior#isFullyStarted() 111 */ 112 public boolean isFullyStarted() { 113 if (isKernelAlive()) { 114 AbstractNameQuery query = new AbstractNameQuery(PersistentConfigurationList.class.getName()); 115 Set configLists = kernel.listGBeans(query); 116 if (!configLists.isEmpty()) { 117 AbstractName on = (AbstractName) configLists.toArray()[0]; 118 try { 119 Boolean b = (Boolean) kernel.getAttribute(on, "kernelFullyStarted"); 120 return b.booleanValue(); 121 } catch (GBeanNotFoundException e) { 122 // ignore 123 } catch (NoSuchAttributeException e) { 124 // ignore 125 } catch (Exception e) { 126 e.printStackTrace(); 127 } 128 } else { 129 Trace.trace(Trace.INFO, "configLists is empty"); 130 } 131 } 132 return false; 133 } 134 135 public IPath getPublishDirectory(IModule[] module) { 136 if (module == null || module.length == 0) 137 return null; 138 139 if (getGeronimoServer().isRunFromWorkspace()) { 140 // TODO fix me, see if project root, component root, or output 141 // container should be returned 142 return module[module.length - 1].getProject().getLocation(); 143 } else { 144 ClassLoader old = Thread.currentThread().getContextClassLoader(); 145 try { 146 Thread.currentThread().setContextClassLoader(getContextClassLoader()); 147 String configId = getConfigId(module[0]); 148 Artifact artifact = Artifact.create(configId); 149 AbstractName name = Configuration.getConfigurationAbstractName(artifact); 150 GBeanData data = kernel.getGBeanData(name); 151 URL url = (URL) data.getAttribute("baseURL"); 152 return getModulePath(module, url); 153 } catch (Exception e) { 154 Trace.trace(Trace.SEVERE, "Error in getting publish directory", e); 155 } finally { 156 Thread.currentThread().setContextClassLoader(old); 157 } 158 } 159 160 return null; 161 } 162 163 /* 164 * (non-Javadoc) 165 * 166 * @see org.apache.geronimo.st.core.GenericGeronimoServerBehaviour#getContextClassLoader() 167 */ 168 protected ClassLoader getContextClassLoader() { 169 return Kernel.class.getClassLoader(); 170 } 171 172 @Override 173 protected List getOrderedModules(IServer server, List modules, 174 List deltaKind) { 175 DependencyHelper dh = new DependencyHelper(); 176 List list = dh.reorderModules(this.getServer(),modules, deltaKind); 177 return list; 178 } 179 }