001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.tomcat5.nodes;
029:
030: import java.util.List;
031: import java.util.TreeSet;
032: import java.util.logging.Level;
033: import java.util.logging.Logger;
034: import javax.enterprise.deploy.shared.ModuleType;
035: import javax.enterprise.deploy.spi.DeploymentManager;
036: import javax.enterprise.deploy.spi.Target;
037: import javax.enterprise.deploy.spi.TargetModuleID;
038: import org.netbeans.modules.tomcat5.AuthorizationException;
039: import org.netbeans.modules.tomcat5.TomcatManager;
040: import org.netbeans.modules.tomcat5.TomcatModule;
041: import org.openide.DialogDisplayer;
042: import org.openide.NotifyDescriptor;
043: import org.openide.nodes.ChildFactory;
044: import org.openide.nodes.Node;
045: import org.openide.util.Lookup;
046: import org.openide.util.NbBundle;
047:
048: /**
049: * Factory for the children of the web module node (children are web apps).
050: * Expects {@link TomcatManager} and {@link Target} in lookup to operate
051: * correctly.
052: * <p>
053: * If the {@link ChildrenFactory} is thread safe, this class is thread safe too.
054: *
055: * @author Petr Hejl
056: */
057: public class TomcatWebModuleChildrenFactory extends
058: ChildFactory<TomcatWebModule> {
059:
060: private static final TomcatWebModule MODULE_WAITING_MARK = new TomcatWebModule(
061: null, null, false);
062:
063: private static final Logger LOGGER = Logger
064: .getLogger(TomcatWebModuleChildrenFactory.class.getName());
065:
066: private final Lookup lookup;
067:
068: /**
069: * Constructs the factory.
070: *
071: * @param lookup lookup where the target and manager is available
072: */
073: public TomcatWebModuleChildrenFactory(Lookup lookup) {
074: this .lookup = lookup;
075: }
076:
077: /**
078: * Updates the keys and refreshes nodes.
079: */
080: public void updateKeys() {
081: refresh(false);
082: }
083:
084: /**
085: * {@inheriDoc}
086: */
087: @Override
088: protected Node createNodeForKey(TomcatWebModule key) {
089: if (key == MODULE_WAITING_MARK) {
090: return createWaitNode();
091: }
092:
093: TomcatWebModuleNode node = new TomcatWebModuleNode(key);
094: key.setRepresentedNode(node);
095: return node;
096: }
097:
098: /**
099: * {@inheritDoc}
100: * <p>
101: * Asks the tomcat manager for modules available on the target. Manager
102: * and target are fetched from lookup passed in constructor.
103: */
104: @Override
105: protected boolean createKeys(List<TomcatWebModule> toPopulate) {
106: DeploymentManager manager = lookup
107: .lookup(DeploymentManager.class);
108: Target target = lookup.lookup(Target.class);
109:
110: TreeSet<TomcatWebModule> list = new TreeSet<TomcatWebModule>(
111: TomcatWebModule.TOMCAT_WEB_MODULE_COMPARATOR);
112:
113: if (manager instanceof TomcatManager && target != null) {
114: TomcatManager tm = (TomcatManager) manager;
115:
116: if (tm.isSuspended() || !tm.isRunning(true)) {
117: return true;
118: }
119: try {
120: TargetModuleID[] modules = manager.getRunningModules(
121: ModuleType.WAR, new Target[] { target });
122: for (int i = 0; i < modules.length; i++) {
123: list.add(new TomcatWebModule(manager,
124: (TomcatModule) modules[i], true));
125: }
126:
127: modules = manager.getNonRunningModules(ModuleType.WAR,
128: new Target[] { target });
129: for (int i = 0; i < modules.length; i++) {
130: list.add(new TomcatWebModule(manager,
131: (TomcatModule) modules[i], false));
132: }
133:
134: } catch (Exception e) {
135: if (e.getCause() instanceof AuthorizationException) {
136: // connection to tomcat manager has not been allowed
137: String errMsg = NbBundle.getMessage(
138: TomcatWebModuleChildrenFactory.class,
139: "MSG_AuthorizationFailed");
140: NotifyDescriptor notDesc = new NotifyDescriptor.Message(
141: errMsg, NotifyDescriptor.ERROR_MESSAGE);
142: DialogDisplayer.getDefault().notify(notDesc);
143: } else {
144: LOGGER.log(Level.INFO, null, e);
145: }
146: }
147: }
148: toPopulate.addAll(list);
149: return true;
150: }
151:
152: }
|