/projects/netbeans-7.3/j2eeserver/src/org/netbeans/modules/j2ee/deployment/impl/ServerInstance.java
Java | 1262 lines | 911 code | 124 blank | 227 comment | 214 complexity | 7482f1add1d579b0eeaf4e4d54ff2790 MD5 | raw file
- /*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
- *
- * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
- * Other names may be trademarks of their respective owners.
- *
- * The contents of this file are subject to the terms of either the GNU
- * General Public License Version 2 only ("GPL") or the Common
- * Development and Distribution License("CDDL") (collectively, the
- * "License"). You may not use this file except in compliance with the
- * License. You can obtain a copy of the License at
- * http://www.netbeans.org/cddl-gplv2.html
- * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
- * specific language governing permissions and limitations under the
- * License. When distributing the software, include this License Header
- * Notice in each file and include the License file at
- * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the GPL Version 2 section of the License file that
- * accompanied this code. If applicable, add the following below the
- * License Header, with the fields enclosed by brackets [] replaced by
- * your own identifying information:
- * "Portions Copyrighted [year] [name of copyright owner]"
- *
- * Contributor(s):
- *
- * The Original Software is NetBeans. The Initial Developer of the Original
- * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
- * Microsystems, Inc. All Rights Reserved.
- *
- * If you wish your version of this file to be governed by only the CDDL
- * or only the GPL Version 2, indicate your decision by adding
- * "[Contributor] elects to include this software in this distribution
- * under the [CDDL or GPL Version 2] license." If you do not indicate a
- * single choice of license, a recipient has the option to distribute
- * your version of this file under either the CDDL, the GPL Version 2 or
- * to extend the choice of license to its licensees as provided above.
- * However, if you add GPL Version 2 code and therefore, elected the GPL
- * Version 2 license, then the option applies only if the new code is
- * made subject to such option by the copyright holder.
- */
- package org.netbeans.modules.j2ee.deployment.impl;
- import java.beans.PropertyChangeEvent;
- import java.beans.PropertyChangeListener;
- import java.net.InetAddress;
- import java.net.NetworkInterface;
- import java.net.SocketException;
- import java.net.UnknownHostException;
- import javax.enterprise.deploy.spi.*;
- import javax.enterprise.deploy.shared.*;
- import org.netbeans.modules.j2ee.deployment.common.api.ConfigurationException;
- import javax.enterprise.deploy.spi.status.*;
- import javax.swing.JButton;
- import javax.swing.SwingUtilities;
- import org.netbeans.api.debugger.DebuggerManager;
- import org.netbeans.api.debugger.DebuggerManagerAdapter;
- import org.netbeans.api.debugger.Session;
- import org.netbeans.api.debugger.jpda.AttachingDICookie;
- import org.netbeans.api.debugger.jpda.JPDADebugger;
- import org.netbeans.modules.j2ee.deployment.common.api.Datasource;
- import org.netbeans.modules.j2ee.deployment.common.api.DatasourceAlreadyExistsException;
- import org.netbeans.modules.j2ee.deployment.devmodules.spi.ArtifactListener.Artifact;
- import org.netbeans.modules.j2ee.deployment.plugins.spi.JDBCDriverDeployer;
- import java.util.*;
- import java.util.concurrent.CopyOnWriteArrayList;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.TimeoutException;
- import java.util.concurrent.atomic.AtomicReference;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
- import javax.swing.event.ChangeListener;
- import org.netbeans.api.annotations.common.NonNull;
- import org.netbeans.api.debugger.LazyDebuggerManagerListener;
- import org.netbeans.modules.j2ee.deployment.common.api.MessageDestination;
- import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
- import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
- import org.netbeans.modules.j2ee.deployment.impl.ui.ProgressUI;
- import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
- import org.netbeans.modules.j2ee.deployment.plugins.spi.J2eePlatformImpl;
- import org.netbeans.modules.j2ee.deployment.plugins.api.ServerDebugInfo;
- import org.netbeans.modules.j2ee.deployment.plugins.api.ServerLibrary;
- import org.netbeans.modules.j2ee.deployment.plugins.api.ServerLibraryDependency;
- import org.netbeans.modules.j2ee.deployment.plugins.spi.StartServer;
- import org.netbeans.modules.j2ee.deployment.plugins.spi.TargetModuleIDResolver;
- import org.netbeans.modules.j2ee.deployment.plugins.api.UISupport;
- import org.netbeans.modules.j2ee.deployment.plugins.spi.AntDeploymentProvider;
- import org.netbeans.modules.j2ee.deployment.plugins.spi.DatasourceManager;
- import org.netbeans.modules.j2ee.deployment.plugins.spi.FindJSPServlet;
- import org.netbeans.modules.j2ee.deployment.plugins.spi.IncrementalDeployment;
- import org.netbeans.modules.j2ee.deployment.plugins.spi.J2eePlatformFactory;
- import org.netbeans.modules.j2ee.deployment.plugins.spi.MessageDestinationDeployment;
- import org.netbeans.modules.j2ee.deployment.plugins.spi.ServerInstanceDescriptor;
- import org.netbeans.modules.j2ee.deployment.plugins.spi.ServerLibraryManager;
- import org.netbeans.modules.j2ee.deployment.profiler.api.ProfilerSupport;
- import org.netbeans.modules.j2ee.deployment.profiler.spi.Profiler;
- import org.openide.nodes.Node;
- import org.openide.util.NbBundle;
- import org.openide.NotifyDescriptor;
- import org.openide.DialogDisplayer;
- import org.openide.util.ChangeSupport;
- import org.openide.util.Exceptions;
- import org.openide.util.Parameters;
- import org.openide.util.RequestProcessor;
- import org.openide.util.WeakListeners;
- import org.openide.windows.InputOutput;
- public class ServerInstance implements Node.Cookie, Comparable {
-
- /** Server state is being checked or state changes is in progress */
- public static final int STATE_WAITING = 1;
- /** Server is stopped */
- public static final int STATE_STOPPED = 2;
- /** Server is running in normal mode */
- public static final int STATE_RUNNING = 3;
- /** Server is running in debug mode */
- public static final int STATE_DEBUGGING = 4;
- /** Server is suspended on a break point (in debug mode and not responding) */
- public static final int STATE_SUSPENDED = 5;
- /** Server is running in profile mode */
- public static final int STATE_PROFILING = 6;
- /** Server is ready for the profiler to connect, server JVM is blocked. */
- public static final int STATE_PROFILER_BLOCKING = 7;
- /** Server is starting in profile mode. */
- public static final int STATE_PROFILER_STARTING = 8;
-
- /** For how long should plugins be allowed to block in the isRunning method */
- private static final int RUNNING_CHECK_TIMEOUT = 10000; // in millis
- /** For how long should plugins be allowed to block in the isDebuggable method */
- private static final int DEBUGGING_CHECK_TIMEOUT = 10000; // in millis
-
- /** Default maximum amount of time the server should finish starting/stopping/deploying in */
- private static final long DEFAULT_TIMEOUT = 1200000; // in millis
-
- private static final Logger LOGGER = Logger.getLogger(ServerInstance.class.getName());
- private static final RequestProcessor REFRESH_PROCESSOR =
- new RequestProcessor("Java EE server registry refresh", 5);
- private static final RequestProcessor DEBUGGER_STATE_PROCESSOR =
- new RequestProcessor("Java EE server debugger state", 1);
- private final String url;
- private final Server server;
- private final boolean nonPeristent;
- private DeploymentManager manager;
- private DeploymentManager disconnectedManager;
- private IncrementalDeployment incrementalDeployment;
- private ServerInstanceDescriptor instanceDescriptor;
- private TargetModuleIDResolver tmidResolver;
- private J2eePlatform j2eePlatform;
- private J2eePlatformImpl j2eePlatformImpl;
- private StartServer startServer;
- private FindJSPServlet findJSPServlet;
- private ServerLibraryManager libraryManager;
- private ServerLibraryManager disconnectedLibraryManager;
- private DatasourceManager dsMgr;
- private DatasourceManager ddsMgr;
- private MessageDestinationDeployment msgDestDeploymentConnected;
- private MessageDestinationDeployment msgDestDeploymentDisconnected;
- private final Set targetsStartedByIde = new HashSet(); // valued by target name
- private Map targets; // keyed by target name, valued by ServerTarget
- private boolean managerStartedByIde = false;
- private ServerTarget coTarget = null;
- private final DeletableInstanceProperties instanceProperties;
- private final HashMap/*<Target, ServerDebugInfo>*/ debugInfo = new HashMap();
-
- // last known server state, the initial value is stopped
- private volatile int serverState = STATE_STOPPED;
- // server state listeners
- private final List<StateListener> stateListeners = new CopyOnWriteArrayList<StateListener>();
-
- // running check helpers
- private long lastCheck = 0;
- private boolean isRunning = false;
-
- private final ChangeSupport managerChangeSupport = new ChangeSupport(this);
-
- private static AtomicReference<ServerInstance> profiledServerInstance = new AtomicReference<ServerInstance>();
-
- private final DebuggerStateListener debuggerStateListener;
-
- // PENDING how to manage connected/disconnected servers with the same manager?
- // maybe concept of 'default unconnected instance' is broken?
- public ServerInstance(Server server, String url, boolean nonPersistent) {
- this.server = server;
- this.url = url;
- this.nonPeristent = nonPersistent;
- instanceProperties = nonPersistent ? new MemoryInstancePropertiesImpl(url)
- : new DefaultInstancePropertiesImpl(url);
- // listen to debugger changes so that we can update server status accordingly
- debuggerStateListener = new DebuggerStateListener();
- DebuggerManager.getDebuggerManager().addDebuggerListener(
- WeakListeners.create(LazyDebuggerManagerListener.class, debuggerStateListener,
- DebuggerManager.getDebuggerManager()));
- }
-
- /** Return this server instance InstanceProperties. */
- public InstanceProperties getInstanceProperties() {
- return instanceProperties;
- }
-
- /** Return display name of this server instance.*/
- public String getDisplayName() {
- return instanceProperties.getProperty(InstanceProperties.DISPLAY_NAME_ATTR);
- }
- public void addManagerChangeListener(ChangeListener listener) {
- managerChangeSupport.addChangeListener(listener);
- }
- public void removeManagerChangeListener(ChangeListener listener) {
- managerChangeSupport.removeChangeListener(listener);
- }
- /**
- * Returns value of the specified timeout propety in milliseconds. If the
- * timeout property is not defined the specified default values is returned.
- *
- * @param propName timeout property
- * @param defaultValue value which will be returned when the specified timeout
- * property is not set.
- *
- * @return value of the specified timeout propety in milliseconds.
- */
- private long getTimeout(String propName, long defaultValue) {
- long returnValue = defaultValue;
- String timeout = instanceProperties.getProperty(propName);
- if (timeout != null) {
- try {
- returnValue = Long.parseLong(timeout) * 1000;
- } catch (NumberFormatException e) {
- LOGGER.log(Level.FINE, "could not parse timeout property", e); // NOI18N
- }
- }
- return returnValue;
- }
-
- /** Get the server startup timeout in milliseconds */
- private long getStartupTimeout() {
- return getTimeout(InstanceProperties.STARTUP_TIMEOUT, DEFAULT_TIMEOUT);
- }
-
- /** Get the server shutdown timeout in milliseconds */
- private long getShutdownTimeout() {
- return getTimeout(InstanceProperties.SHUTDOWN_TIMEOUT, DEFAULT_TIMEOUT);
- }
-
- /** Get the deployment timeout in milliseconds */
- long getDeploymentTimeout() {
- return getTimeout(InstanceProperties.DEPLOYMENT_TIMEOUT, DEFAULT_TIMEOUT);
- }
-
- public Server getServer() {
- return server;
- }
-
- public String getUrl() {
- return url;
- }
-
- public DeploymentManager getDeploymentManager() {
- DeploymentManager managerTmp = null;
- synchronized (this) {
- managerTmp = manager;
- }
- if (managerTmp != null) {
- return managerTmp;
- }
- try {
- if (instanceProperties.isDeleted()) {
- String msg = NbBundle.getMessage(ServerInstance.class, "MSG_InstanceNotExists", url);
- throw new IllegalStateException(msg);
- }
- String username = instanceProperties.getProperty(InstanceProperties.USERNAME_ATTR);
- String password;
- if (nonPeristent) {
- password = instanceProperties.getProperty(InstanceProperties.PASSWORD_ATTR);
- } else {
- password = ServerRegistry.readPassword(url);
- }
- managerTmp = server.getDeploymentManager(url, username, password);
- boolean fire = false;
- synchronized (this) {
- fire = (manager != managerTmp);
- manager = managerTmp;
- }
- if (fire) {
- firePossibleManagerChange();
- }
- } catch(javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException e) {
- throw new RuntimeException(e);
- }
- return managerTmp;
- }
-
- public synchronized boolean isConnected () {
- return manager != null;
- }
-
- public DeploymentManager getDisconnectedDeploymentManager() throws DeploymentManagerCreationException {
- DeploymentManager disconnectedManagerTmp = null;
- synchronized (this) {
- disconnectedManagerTmp = disconnectedManager;
- }
- if (disconnectedManagerTmp != null) {
- return disconnectedManagerTmp;
- }
- if (instanceProperties.isDeleted()) {
- String msg = NbBundle.getMessage(ServerInstance.class, "MSG_InstanceNotExists", url);
- throw new DeploymentManagerCreationException(msg);
- }
- disconnectedManagerTmp = server.getDisconnectedDeploymentManager(url);
- boolean fire = false;
- synchronized (this) {
- fire = (manager == null) && (disconnectedManager != disconnectedManagerTmp);
- disconnectedManager = disconnectedManagerTmp;
- }
- if (fire) {
- firePossibleManagerChange();
- }
- return disconnectedManagerTmp;
- }
-
- public J2eePlatform getJ2eePlatform() {
- return j2eePlatform;
- }
-
- public void setJ2eePlatform(J2eePlatform aJ2eePlatform ) {
- j2eePlatform = aJ2eePlatform;
- }
-
- public J2eePlatformImpl getJ2eePlatformImpl() {
- if (j2eePlatformImpl == null) {
- J2eePlatformFactory fact = server.getJ2eePlatformFactory();
- // TODO this will be removed, implementation of J2EEPlatformFactory will be mandatory
- if (fact != null) {
- try {
- j2eePlatformImpl = fact.getJ2eePlatformImpl(isConnected() ? getDeploymentManager() : getDisconnectedDeploymentManager());
- } catch (DeploymentManagerCreationException dmce) {
- // this condition is ugly workaround for disconnected
- // deployment manager throwing exception - bug 113907
- if (!instanceProperties.isDeleted()) {
- Exceptions.printStackTrace(dmce);
- }
- }
- }
- }
- return j2eePlatformImpl;
- }
-
- public ServerDebugInfo getServerDebugInfo(Target target) {
- assert debugInfo != null;
- ServerDebugInfo sdi = null;
- if (target == null) { //performance: treat as special simple case
- sdi = (ServerDebugInfo) debugInfo.get(null);
- } else {
- for (Iterator it = debugInfo.keySet().iterator(); sdi == null && it.hasNext(); ) {
- Target t = (Target) it.next();
- if (t == target || (t != null && t.getName().equals(target.getName()))) {
- sdi = (ServerDebugInfo) debugInfo.get(t);
- }
- }
- }
-
- return sdi;
- }
-
- public void refresh() {
- REFRESH_PROCESSOR.post(new Runnable() {
- @Override
- public void run() {
- try {
- int oldState = getServerState();
- setServerState(STATE_WAITING);
- if (ServerInstance.this == profiledServerInstance.get()) {
- updateStateFromProfiler();
- return;
- }
- if (isSuspended()) {
- setServerState(ServerInstance.STATE_SUSPENDED);
- } else if (isDebuggable(null)) {
- if (oldState != ServerInstance.STATE_SUSPENDED) {
- // this will decrease the possibility of accessing server
- // when it is in suspended mode when we might freeze
- reset();
- }
- initCoTarget();
- setServerState(ServerInstance.STATE_DEBUGGING);
- } else if (isReallyRunning()) {
- reset();
- initCoTarget();
- setServerState(ServerInstance.STATE_RUNNING);
- } else {
- reset();
- setServerState(ServerInstance.STATE_STOPPED);
- }
- } finally {
- // safety catch - make sure that we are not still waiting
- if (getServerState() == STATE_WAITING) {
- setServerState(ServerInstance.STATE_STOPPED);
- }
- }
- }
- });
- }
-
- public void reset() {
- DeploymentManager managerTmp = null;
- synchronized (this) {
- managerTmp = manager;
- manager = null;
- disconnectedManager = null;
- incrementalDeployment = null;
- tmidResolver = null;
- startServer = null;
- findJSPServlet = null;
- coTarget = null;
- targets = null;
- }
- firePossibleManagerChange();
-
- if (managerTmp != null) {
- managerTmp.release();
- }
- }
-
- /** Remove this server instance and stop it if it has been started from within the IDE */
- public void remove() {
- DebuggerManager.getDebuggerManager().removeDebuggerListener(debuggerStateListener);
- stopIfStartedByIde();
- // close the server io window
- if (getUrl() != null) {
- InputOutput io = UISupport.getServerIO(url);
- if (io != null && !io.isClosed()) {
- io.closeInputOutput();
- }
- ServerRegistry.getInstance().removeServerInstance(getUrl());
- } else {
- LOGGER.log(Level.WARNING, "Trying to remove {0}, but url is null", server != null ? server.getShortName() : null);
- }
- }
-
- /** Stop the server if it has been started from within the IDE, do nothing otherwise */
- public void stopIfStartedByIde() {
- if (managerStartedByIde) {
- if (canStopDontWait()) {
- stopDontWait();
- } else {
- String title = NbBundle.getMessage(ServerInstance.class, "LBL_ShutDownServer", getDisplayName());
- final ProgressUI progressUI = new ProgressUI(title, true, null);
- progressUI.start();
- RequestProcessor.getDefault().post(new Runnable() {
- public void run() {
- try {
- for (Iterator it = targetsStartedByIde.iterator(); it.hasNext();) {
- ServerTarget serverTarget = getServerTarget((String)it.next());
- if (serverTarget != null) {
- try {
- _stop(serverTarget.getTarget(), progressUI);
- } catch (ServerException ex) {
- Logger.getLogger("global").log(Level.INFO, null, ex);
- }
- }
- }
- if (isReallyRunning() || isSuspended()) {
- try {
- _stop(progressUI);
- } catch (ServerException ex) {
- Logger.getLogger("global").log(Level.INFO, null, ex);
- }
- }
- } finally {
- progressUI.finish();
- }
- }
- });
- progressUI.showProgressDialog();
- }
- }
- }
-
- /** Set the server state and notify all listeners */
- public void setServerState(int newState) {
- int oldState = serverState;
- serverState = newState;
- fireStateChanged(oldState, newState);
- }
-
- /** Return the current server state */
- public int getServerState() {
- return serverState;
- }
-
- /** Is it forbidden to remove this server instance from the server registry? */
- public boolean isRemoveForbidden() {
- String removeForbid = instanceProperties.getProperty(InstanceProperties.REMOVE_FORBIDDEN);
- return Boolean.valueOf(removeForbid).booleanValue();
- }
- /**
- * <i>This method can have ugly side effect of starting the server.</i>
- *
- * @return
- */
- public ServerTarget[] getTargets() {
- Map targets = getTargetMap();
- synchronized (this) {
- return (ServerTarget[]) targets.values().toArray(new ServerTarget[targets.size()]);
- }
- }
-
- public Collection getTargetList() {
- Map targets = getTargetMap();
- synchronized (this) {
- return targets.values();
- }
- }
-
- // PENDING use targets final variable?
- private Map getTargetMap() {
- Map tmpTargets = null;
- synchronized (this) {
- tmpTargets = targets;
- }
- if (tmpTargets == null || tmpTargets.size() < 1) {
- Target[] targs = null;
- StartServer startServer = getStartServer();
- // TODO revert the Glassfish /w profiler related workaround
- // once profiling uses the same startup sequence as debugging eg.
- try {
- if (! isRunning() && startServer != null && startServer.needsStartForTargetList()) {
- // GF workaround
- // the guard condition introduced
- // *** original line
- // start();
- // ***
- if (ProfilerSupport.getState() == ProfilerSupport.STATE_INACTIVE) {
- start();
- }
- // end of GF workaround
- }
- // GF workaround
- // a dealy loop introduced
- // *** original line
- // targs = getDeploymentManager().getTargets();
- // ***
- do {
- targs = getDeploymentManager().getTargets();
- if (targs == null && ProfilerSupport.getState() == ProfilerSupport.STATE_PROFILING) {
- try {
- Thread.sleep(500);
- } catch (InterruptedException ex) {
- Thread.currentThread().interrupt();
- }
- }
- } while (targs == null && ProfilerSupport.getState() == ProfilerSupport.STATE_PROFILING);
- // end of GF workaround
- } catch(IllegalStateException e) {
- LOGGER.log(Level.INFO, null, e);
- }
- if (targs == null) {
- targs = new Target[0];
- }
-
- tmpTargets = new HashMap();
- for (int i = 0; i < targs.length; i++) {
- tmpTargets.put(targs[i].getName(), new ServerTarget(this, targs[i]));
- }
- synchronized (this) {
- targets = tmpTargets;
- }
- }
- return tmpTargets;
- }
-
- public ServerTarget getServerTarget(String targetName) {
- return (ServerTarget) getTargetMap().get(targetName);
- }
-
- public Target getTarget(String targetName) {
- return getServerTarget(targetName).getTarget();
- }
-
- public StartServer getStartServer() {
- DeploymentManager dm = null;
- try {
- dm = getDisconnectedDeploymentManager();
- } catch (DeploymentManagerCreationException dmce) {
- Logger.getLogger(ServerInstance.class.getName()).log(Level.INFO, null, dmce);
- return null;
- }
- synchronized (this) {
- if (startServer == null) {
- startServer = server.getOptionalFactory().getStartServer(dm);
- }
- return startServer;
- }
- }
-
- public IncrementalDeployment getIncrementalDeployment() {
- DeploymentManager dm = getDeploymentManager();
- synchronized (this) {
- if (incrementalDeployment == null) {
- incrementalDeployment = server.getOptionalFactory().getIncrementalDeployment(dm);
- }
- return incrementalDeployment;
- }
- }
-
- public ServerInstanceDescriptor getServerInstanceDescriptor() {
- DeploymentManager dm = getDeploymentManager();
- synchronized (this) {
- if (instanceDescriptor == null) {
- instanceDescriptor = server.getOptionalFactory().getServerInstanceDescriptor(dm);
- }
- return instanceDescriptor;
- }
- }
- public AntDeploymentProvider getAntDeploymentProvider() {
- try {
- return server.getOptionalFactory().getAntDeploymentProvider(getDisconnectedDeploymentManager());
- } catch (DeploymentManagerCreationException ex) {
- Logger.getLogger("global").log(Level.INFO, null, ex);
- return null;
- }
- }
-
- public JDBCDriverDeployer getJDBCDriverDeployer() {
- try {
- return server.getOptionalFactory().getJDBCDriverDeployer(getDisconnectedDeploymentManager());
- } catch (DeploymentManagerCreationException ex) {
- Logger.getLogger("global").log(Level.INFO, null, ex);
- return null;
- }
- }
-
- public TargetModuleIDResolver getTargetModuleIDResolver() {
- DeploymentManager dm = getDeploymentManager();
- synchronized (this) {
- if (tmidResolver == null) {
- tmidResolver = server.getOptionalFactory().getTargetModuleIDResolver(dm);
- }
- return tmidResolver;
- }
- }
-
- public FindJSPServlet getFindJSPServlet() {
- DeploymentManager dm = null;
- try {
- dm = getDisconnectedDeploymentManager();
- } catch (DeploymentManagerCreationException dmce) {
- throw new RuntimeException(dmce);
- }
- synchronized (this) {
- if (findJSPServlet == null) {
- findJSPServlet = server.getOptionalFactory().getFindJSPServlet(dm);
- }
- return findJSPServlet;
- }
- }
-
- private DatasourceManager getDatasourceManager() {
- DeploymentManager dm = getDeploymentManager();
- synchronized (this) {
- if (dsMgr == null) {
- dsMgr = server.getOptionalFactory().getDatasourceManager(dm);
- }
- return dsMgr;
- }
- }
-
- private DatasourceManager getDisconnectedDatasourceManager() {
- DeploymentManager dm = null;
- try {
- dm = getDisconnectedDeploymentManager();
- } catch (DeploymentManagerCreationException dmce) {
- throw new RuntimeException(dmce);
- }
- synchronized (this) {
- if (ddsMgr == null) {
- ddsMgr = server.getOptionalFactory().getDatasourceManager(dm);
- }
- return ddsMgr;
- }
- }
- private ServerLibraryManager getServerLibraryManager() {
- DeploymentManager dm = getDeploymentManager();
- synchronized (this) {
- if (libraryManager == null) {
- libraryManager = server.getOptionalFactory().getServerLibraryManager(dm);
- }
- return libraryManager;
- }
- }
- private ServerLibraryManager getDisconnectedServerLibraryManager() {
- DeploymentManager dm = null;
- try {
- dm = getDisconnectedDeploymentManager();
- } catch (DeploymentManagerCreationException dmce) {
- throw new RuntimeException(dmce);
- }
- synchronized (this) {
- if (disconnectedLibraryManager == null) {
- disconnectedLibraryManager = server.getOptionalFactory().getServerLibraryManager(dm);
- }
- return disconnectedLibraryManager;
- }
- }
- /**
- * Gets the data sources deployed on the this server instance.
- *
- * @return set of data sources
- */
- public Set<Datasource> getDatasources() throws ConfigurationException {
-
- DatasourceManager ddsMgr = getDisconnectedDatasourceManager();
-
- Set deployedDS = Collections.<Datasource>emptySet();
- if (ddsMgr != null)
- deployedDS = ddsMgr.getDatasources();
-
- return deployedDS;
- }
-
- /**
- * Deploys data sources saved in the module.
- *
- * @exception ConfigurationException if there is some problem with data source configuration
- * @exception DatasourceAlreadyExistsException if module data source(s) are conflicting
- * with data source(s) already deployed on the server
- */
- public void deployDatasources(Set<Datasource> datasources) throws ConfigurationException, DatasourceAlreadyExistsException {
-
- DatasourceManager dsMgr = getDatasourceManager();
- if (dsMgr != null)
- dsMgr.deployDatasources(datasources);
- }
- public boolean isServerLibraryManagementSupported() {
- return getDisconnectedServerLibraryManager() != null;
- }
- public Set<ServerLibrary> getDeployableLibraries() {
- ServerLibraryManager libraryManager = getDisconnectedServerLibraryManager();
- Set<ServerLibrary> libraries = Collections.emptySet();
- if (libraryManager != null) {
- libraries = libraryManager.getDeployableLibraries();
- }
- return libraries;
- }
- public Set<ServerLibrary> getDeployedLibraries() {
- ServerLibraryManager libraryManager = getDisconnectedServerLibraryManager();
- Set<ServerLibrary> libraries = Collections.emptySet();
- if (libraryManager != null) {
- libraries = libraryManager.getDeployedLibraries();
- }
- return libraries;
- }
- public Set<ServerLibraryDependency> getDeployableDependencies(
- @NonNull Set<ServerLibraryDependency> dependencies) {
- ServerLibraryManager libraryManager = getDisconnectedServerLibraryManager();
- Set<ServerLibraryDependency> result = Collections.emptySet();
- if (libraryManager != null) {
- result = libraryManager.getDeployableDependencies(dependencies);
- }
- return result;
- }
- public Set<ServerLibraryDependency> getMissingDependencies(
- @NonNull Set<ServerLibraryDependency> dependencies) {
- ServerLibraryManager libraryManager = getDisconnectedServerLibraryManager();
- Set<ServerLibraryDependency> result = Collections.emptySet();
- if (libraryManager != null) {
- result = libraryManager.getMissingDependencies(dependencies);
- }
- return result;
- }
- public void deployLibraries(Set<ServerLibraryDependency> libraries) throws ConfigurationException {
- ServerLibraryManager libraryManager = getServerLibraryManager();
- if (libraryManager != null) {
- StartServer ss = getStartServer();
- if (ss != null && !ss.isRunning() && ss.needsStartForAdminConfig()) {
- start();
- }
- libraryManager.deployLibraries(libraries);
- }
- }
- private synchronized MessageDestinationDeployment getMessageDestinationDeploymentConnected() {
- if (msgDestDeploymentConnected == null) {
- msgDestDeploymentConnected = server.getOptionalFactory().
- getMessageDestinationDeployment(getDeploymentManager());
- }
- return msgDestDeploymentConnected;
- }
-
- private MessageDestinationDeployment getMessageDestinationDeploymentDisconnected() {
- DeploymentManager dm = null;
- try {
- dm = getDisconnectedDeploymentManager();
- } catch (DeploymentManagerCreationException dmce) {
- throw new RuntimeException(dmce);
- }
- synchronized (this) {
- if (msgDestDeploymentDisconnected == null) {
- msgDestDeploymentDisconnected = server.getOptionalFactory().getMessageDestinationDeployment(dm);
- }
- return msgDestDeploymentDisconnected;
- }
- }
-
- /**
- * Retrieves message destinations configured on the target server instance.
- *
- * @return set of message destinations
- *
- * @throws ConfigurationException if there is some problem with message destination configuration
- */
- public Set<MessageDestination> getMessageDestinations() throws ConfigurationException {
-
- MessageDestinationDeployment destDepl = getMessageDestinationDeploymentDisconnected();
- if (destDepl != null) {
- return destDepl.getMessageDestinations();
- }
-
- return Collections.<MessageDestination>emptySet();
- }
-
- /**
- * Deploys message destinations saved in the module.
- *
- * @param destinations set of message destinations
- *
- * @throws NullPointerException if destinations parameter is null
- * @throws ConfigurationException if there is some problem with message destination configuration
- */
- public void deployMessageDestinations(Set<MessageDestination> destinations) throws ConfigurationException {
-
- Parameters.notNull("destinations", destinations);
-
- MessageDestinationDeployment destDepl = getMessageDestinationDeploymentConnected();
- if (destDepl != null) {
- destDepl.deployMessageDestinations(destinations);
- }
- }
-
- //---------- State API's: running, debuggable, startedByIDE -----------
-
- public boolean isRunningLastCheck() {
- if (lastCheck > 0) {
- return isRunning;
- } else {
- return false;
- }
- }
-
- public boolean isReallyRunning() {
- return isRunningWithinMillis(0);
- }
-
- public boolean isRunning() {
- return isRunningWithinMillis(2000);
- }
-
- public boolean isRunningWithinMillis(long millisecs) {
- if (System.currentTimeMillis() - lastCheck < millisecs) {
- return isRunning;
- }
- final StartServer ss = getStartServer();
- if (ss != null) {
- isRunning = safeTrueTest(new SafeTrueTest() {
- public void run() {
- setResult(ss.isRunning());
- }
- },
- RUNNING_CHECK_TIMEOUT);
- } else {
- isRunning = false;
- }
- lastCheck = System.currentTimeMillis();
- return isRunning;
- }
-
- public boolean isDebuggable(final Target target) {
- final StartServer ss = getStartServer();
- if (ss != null) {
- return safeTrueTest(new SafeTrueTest() {
- public void run() {
- setResult(ss.isDebuggable(target));
- }
- },
- DEBUGGING_CHECK_TIMEOUT);
- } else {
- return false;
- }
- }
-
- /**
- * @return conflict data instance for server instance running in debug mode with the same socket number
- * or shared memory id. If no such server instance exists then null is returned.
- */
- public ConflictData anotherServerDebuggable(Target target) {
-
- ConflictData cd = null;
- //get debug info for this instance
- StartServer thisSS = getStartServer();
- if (thisSS == null) //this case should not occur =>
- return null; //attempt to start server (serverInstance remains null)
- ServerDebugInfo thisSDI = getServerDebugInfo(target);
- if (thisSDI == null) {
- Target t = _retrieveTarget(target);
- thisSDI = thisSS.getDebugInfo(t);
- if (thisSDI == null) {
- LOGGER.log(Level.FINE, "DebuggerInfo cannot be found for: " + this.toString());
- return null;
- }
- }
-
- //get all server instances
- ServerInstance[] serverInstances = ServerRegistry.getInstance().getServerInstances();
- //check existence of a server instance running in debug mode with the same parameters
- for (int i = 0; cd == null && i < serverInstances.length; i++) {
- ServerInstance si = serverInstances[i];
- if (url.equalsIgnoreCase(si.getUrl())) continue;
- if (si.isDebuggable(null)) { //running in debug mode
- Target t = si._retrieveTarget(null);
- ServerDebugInfo sdi = si.getServerDebugInfo(t);
- if (sdi == null) continue; //should not occur -> workaround for issue #56714
- if (thisSDI.getTransport().equals(sdi.getTransport())) { //transport matches
- if (thisSDI.getTransport() == ServerDebugInfo.TRANSPORT_SOCKET) {
- if (thisSDI.getHost().equalsIgnoreCase(sdi.getHost())) //host matches
- if (thisSDI.getPort() == sdi.getPort()) //port matches
- cd = new ConflictData(si, thisSDI);
- } else if (thisSDI.getShmemName().equalsIgnoreCase(sdi.getShmemName()))
- cd = new ConflictData(si, thisSDI);
- }
- }
- }
-
- return cd;
- }
- private JPDADebugger getDebugger() {
- Session[] sessions = DebuggerManager.getDebuggerManager().getSessions();
- Target target = _retrieveTarget(null);
- ServerDebugInfo sdi = getServerDebugInfo(target);
- if (sdi == null) {
- LOGGER.log(Level.FINE, "DebuggerInfo cannot be found for: " + this.toString());
- return null; // give user a chance to start server even if we don't know whether she will success
- }
- for (int i = 0; i < sessions.length; i++) {
- Session s = sessions[i];
- if (s == null) {
- continue;
- }
- Object o = s.lookupFirst(null, AttachingDICookie.class);
- if (o == null) {
- continue;
- }
- AttachingDICookie attCookie = (AttachingDICookie) o;
- if (sdi.getTransport().equals(ServerDebugInfo.TRANSPORT_SHMEM)) {
- String shmem = attCookie.getSharedMemoryName();
- if (shmem == null) {
- continue;
- }
- if (shmem.equalsIgnoreCase(sdi.getShmemName())) {
- Object d = s.lookupFirst(null, JPDADebugger.class);
- if (d != null) {
- return (JPDADebugger) d;
- }
- }
- } else {
- String host = attCookie.getHostName();
- if (host != null && isSameHost(host, sdi.getHost())
- && attCookie.getPortNumber() == sdi.getPort()) {
- Object d = s.lookupFirst(null, JPDADebugger.class);
- if (d != null) {
- return (JPDADebugger) d;
- }
- }
- }
- }
- return null;
- }
-
- /**
- * Returns true if this server is started in debug mode AND debugger is attached to it
- * AND threads are suspended (e.g. debugger stopped on breakpoint)
- */
- public boolean isSuspended() {
- JPDADebugger jpda = getDebugger();
- if (jpda == null) {
- return false;
- }
- return jpda.getState() == JPDADebugger.STATE_STOPPED;
- }
- /**
- * Can be this server started in the debug mode? Currently the only case when
- * the server cannot be started in the debugged is when the admin server is
- * not also the target server.
- *
- * @return <code>true</code> if the server can be started in the debug mode,
- * <code>false/code> otherwise.
- */
- public boolean isDebugSupported() {
- StartServer ss = getStartServer();
- if (ss == null) {
- return false;
- }
- return ss.supportsStartDebugging(null);
- }
- /**
- * Can be this server started in profile mode? Currently the only case when
- * the server cannot be started in the debugged is when the admin server is
- * not also the target server.
- */
- public boolean isProfileSupported() {
- Profiler profiler = ServerRegistry.getProfiler();
- if (profiler == null) {
- return false;
- }
- StartServer ss = getStartServer();
- if (ss == null) {
- return false;
- }
- return ss.supportsStartProfiling(null);
- }
-
- /**
- * Return set of ServerTarget's that have been started from inside IDE.
- * @return set of ServerTarget objects.
- */
- public Set getTargetsStartedByIde() {
- Set ret = new HashSet();
- for (Iterator i=targetsStartedByIde.iterator(); i.hasNext(); ) {
- String targetName = (String) i.next();
- ret.add(getServerTarget(targetName));
- }
- return ret;
- }
-
- //----------- State Transistion API's: ----------------------
-
- /**
- * Start the admin server. Show UI feedback.
- * Note: for debug mode, always use startDebugTarget() calls because
- * it is sure then the target need to be started.
- *
- * @throws ServerException if the server cannot be started.
- */
- public void start(ProgressUI ui) throws ServerException {
- try {
- setServerState(STATE_WAITING);
- startTarget(null, ui);
- } finally {
- refresh();
- }
- }
-
- /** Start the admin server in the debug mode. Show UI feedback.
- *
- * @throws ServerException if the server cannot be started.
- */
- public void startDebug(ProgressUI ui) throws ServerException {
- try {
- setServerState(STATE_WAITING);
- startTarget(null, ui, Deployment.Mode.DEBUG);
- _retrieveDebugInfo(null);
- } finally {
- refresh();
- }
- }
-
- /** Start the admin server in the profile mode. Show UI feedback.
- *
- * @throws ServerException if the server cannot be started.
- */
- public void startProfile(boolean forceRestart, ProgressUI ui)
- throws ServerException {
- // check whether another server not already running in profile mode
- // and ask whether it is ok to stop it
- ServerInstance tmpProfiledServerInstance = profiledServerInstance.get();
- if (tmpProfiledServerInstance != null && tmpProfiledServerInstance != this) {
- String msg = NbBundle.getMessage(
- ServerInstance.class,
- "MSG_AnotherServerProfiling",
- tmpProfiledServerInstance.getDisplayName());
- NotifyDescriptor nd = new NotifyDescriptor.Confirmation(msg, NotifyDescriptor.OK_CANCEL_OPTION);
- if (DialogDisplayer.getDefault().notify(nd) == NotifyDescriptor.CANCEL_OPTION) {
- // start in profile mode has been cancelled
- String err = NbBundle.getMessage(ServerInstance.class, "MSG_ProfilingCancelled", getDisplayName());
- throw new ServerException(err);
- }
- }
- try {
- setServerState(STATE_WAITING);
- // target == null - admin server
- _startProfile(null, forceRestart, ui);
- } finally {
- refresh();
- }
- }
-
- /** Restart the admin server in the mode the server was running in before.
- * Show UI feedback.
- *
- * @throws ServerException if the server cannot be restarted.
- */
- public void restart(ProgressUI ui) throws ServerException {
- try {
- setServerState(STATE_WAITING);
- boolean inDebug = isDebuggable(null);
- boolean inProfile = profiledServerInstance.get() == this;
- boolean stopped = true;
-
- if (inProfile || isReallyRunning() || isSuspended()) {
- _stop(ui);
- }
- if (stopped) {
- // restart in the mode the server was running in before
- if (inProfile) {
- _startProfile(null, true, ui);
- } else if (inDebug) {
- startDebugTarget(null, ui);
- } else {
- startTarget(null, ui);
- }
- }
- } finally {
- refresh();
- }
- }
-
- /** Stop admin server. Show UI feedback.
- *
- * @throws ServerException if the server cannot be stopped.
- */
- public void stop(ProgressUI ui) throws ServerException {
- try {
- setServerState(STATE_WAITING);
- if (profiledServerInstance.get() == this || isReallyRunning() || isSuspended()) {
- _stop(ui);
- }
- debugInfo.clear();
- } finally {
- refresh();
- }
- }
-
- // Note: configuration needs
- /**
- * Return a connected DeploymentManager if needed by server platform for configuration
- * @return DeploymentManager object for configuration.
- */
- public DeploymentManager getDeploymentManagerForConfiguration() throws DeploymentManagerCreationException {
- StartServer ss = getStartServer();
- if (ss != null && ss.needsStartForConfigure()) {
- start();
- return getDeploymentManager();
- } else {
- return getDisconnectedDeploymentManager();
- }
- }
-
- // Note: execution only need these 3 state transition APIs
-
- /**
- * Start specified target server. If it is also admin server only make sure
- * admin server is running.
- * @param target target server to be started
- * @param ui DeployProgressUI to display start progress
- *
- * @throws ServerException if the target cannot be started.
- */
- public void startTarget(Target target, ProgressUI ui) throws ServerException {
- startTarget(target, ui, Deployment.Mode.RUN);
- }
-
- /**
- * Start specified target server in debug mode. If target is also admin
- * server only make sure admin server is running.
- * @param target target server to be started
- * @param ui DeployProgressUI to display start progress
- *
- * @throws ServerException if the server cannot be started.
- */
- public void startDebugTarget(Target target, ProgressUI ui) throws ServerException {
- startTarget(target, ui, Deployment.Mode.DEBUG);
- _retrieveDebugInfo(target);
- }
-
- /**
- * Start admin server, mainly for registry actions with no existing progress UI
- *
- * @throws ServerException if the server cannot be started.
- */
- private void start() {
- if (SwingUtilities.isEventDispatchThread()) {
- //PENDING maybe a modal dialog instead of async is needed here
- RequestProcessor.getDefault().post(new Runnable() {
- public void run() {
- start();
- }
- });
- return;
- }
- if (isRunning()) {
- return;
- }
- String title = NbBundle.getMessage(ServerInstance.class, "LBL_StartServerProgressMonitor", getDisplayName());
- ProgressUI ui = new ProgressUI(title, false);
- try {
- ui.start();
- start(ui);
- } catch (ServerException ex) {
- Logger.getLogger("global").log(Level.INFO, null, ex);
- } finally {
- ui.finish();
- }
- }
-
- /**
- * Start admin server for profiling, mainly for registry actions with no existing progress UI
- * @param settings settings that will be used to start the server
- *
- * @throws ServerException if the server cannot be started.
- */
- public boolean startProfile(boolean forceRestart, Deployment.Logger logger) {
- Str