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: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.php.rt.providers.impl.local;
043:
044: import org.netbeans.modules.php.rt.providers.impl.local.nodes.LocalServerNode;
045: import java.io.IOException;
046: import java.util.Collections;
047: import java.util.Enumeration;
048: import java.util.List;
049: import java.util.logging.Level;
050: import java.util.logging.Logger;
051:
052: import org.netbeans.modules.php.rt.providers.impl.AbstractProvider;
053: import org.netbeans.modules.php.rt.providers.impl.HostImpl;
054: import org.netbeans.modules.php.rt.spi.providers.CommandProvider;
055: import org.netbeans.modules.php.rt.spi.providers.Host;
056: import org.netbeans.modules.php.rt.spi.providers.ProjectConfigProvider;
057: import org.netbeans.modules.php.rt.spi.providers.UiConfigProvider;
058: import org.openide.filesystems.FileObject;
059: import org.openide.filesystems.FileUtil;
060: import org.openide.nodes.Node;
061: import org.openide.util.NbBundle;
062:
063: /**
064: * @author ads
065: *
066: */
067: public class LocalServerProvider extends
068: AbstractProvider<LocalHostImpl> {
069:
070: private static final String DESCRIPTION = "TXT_LocalServerDescription"; // NOI18N
071:
072: private static final String TYPE_NAME = "TXT_LocalServerTypeName"; // NOI18N
073:
074: private static final String DOCUMENT_ROOT = "documentRoot"; // NOI18N
075:
076: private static final String WEB_SERVER_CONFIG_FILE = "webServerConfig"; // NOI18N
077:
078: private static final String PHP_CONFIG_FILE = "phpConfig"; // NOI18N
079:
080: private static Logger LOGGER = Logger
081: .getLogger(AbstractProvider.class.getName());
082:
083: public LocalServerProvider() {
084: myConfig = new LocalUiConfigProvider(this );
085: myProjectProvider = new LocalProjectConfigProvider(this );
086: myCommandProvider = new LocalCommandProvider(this );
087: }
088:
089: /* (non-Javadoc)
090: * @see org.netbeans.modules.php.rt.spi.providers.WebServerProvider#getConfigProvider()
091: */
092: public UiConfigProvider getConfigProvider() {
093: return myConfig;
094: }
095:
096: /* (non-Javadoc)
097: * @see org.netbeans.modules.php.rt.spi.providers.WebServerProvider#getTypeName()
098: */
099: public String getTypeName() {
100: return NbBundle
101: .getMessage(LocalServerProvider.class, TYPE_NAME);
102: }
103:
104: /* (non-Javadoc)
105: * @see org.netbeans.modules.php.rt.spi.providers.WebServerProvider#getCommandProvider()
106: */
107: public CommandProvider getCommandProvider() {
108: return myCommandProvider;
109: }
110:
111: /* (non-Javadoc)
112: * @see org.netbeans.modules.php.rt.spi.providers.WebServerProvider#getDescription()
113: */
114: public String getDescription() {
115: return NbBundle.getMessage(LocalServerProvider.class,
116: DESCRIPTION);
117: }
118:
119: /* (non-Javadoc)
120: * @see org.netbeans.modules.php.rt.spi.providers.WebServerProvider#createNode(org.netbeans.modules.php.rt.spi.providers.Host)
121: */
122: public Node createNode(Host host) {
123: LocalHostImpl hostImpl = (LocalHostImpl) host;
124: String name = hostImpl.getDisplayName();
125: return new LocalServerNode(name, hostImpl);
126: }
127:
128: /* (non-Javadoc)
129: * @see org.netbeans.modules.php.rt.spi.providers.WebServerProvider#getProjectConfigProvider()
130: */
131: public ProjectConfigProvider getProjectConfigProvider() {
132: return myProjectProvider;
133: }
134:
135: protected LocalHostImpl configureHost(FileObject object) {
136: // check if suggested file object contains data for supported host object
137: if (!isLocalHostObject(object.getAttributes())) {
138: return null;
139: }
140:
141: String name = (String) object.getAttribute(NAME);
142:
143: String domain = (String) object.getAttribute(HOST_NAME);
144: String port = (String) object.getAttribute(PORT);
145: String baseDir = (String) object
146: .getAttribute(BASE_DIRECTORY_PATH);
147:
148: String docRoot = (String) object.getAttribute(DOCUMENT_ROOT);
149: String webServerConfig = (String) object
150: .getAttribute(WEB_SERVER_CONFIG_FILE);
151: String phpConfig = (String) object
152: .getAttribute(PHP_CONFIG_FILE);
153:
154: if (port == null) {
155: port = HostImpl.DEFAULT_PORT;
156: }
157:
158: LocalHostImpl impl = new LocalHostImpl(name, domain, port,
159: baseDir, this );
160: impl.setProperty(LocalHostImpl.DOCUMENT_PATH, docRoot);
161: impl
162: .setProperty(LocalHostImpl.WEB_CONFIG_FILE,
163: webServerConfig);
164: impl.setProperty(LocalHostImpl.PHP_CONFIG_FILE, phpConfig);
165: return impl;
166: }
167:
168: @Override
169: protected boolean acceptHost(FileObject fileObject, Host host) {
170: if (host instanceof LocalHostImpl) {
171: if (!super .acceptHost(fileObject, host))
172: return false;
173:
174: return true;
175: } else {
176: return false;
177: }
178: }
179:
180: /* (non-Javadoc)
181: * @see org.netbeans.modules.php.rt.providers.impl.AbstractProvider#addHost(org.netbeans.modules.php.rt.spi.providers.Host)
182: */
183: @Override
184: protected void addHost(LocalHostImpl host) {
185: super .addHost(host);
186: }
187:
188: /* (non-Javadoc)
189: * @see org.netbeans.modules.php.rt.providers.impl.AbstractProvider#serializeAdded(org.netbeans.modules.php.rt.spi.providers.Host)
190: */
191: @Override
192: protected void serializeAdded(LocalHostImpl host) {
193: serializeUpdated(host, host);
194: }
195:
196: /* (non-Javadoc)
197: * @see org.netbeans.modules.php.rt.providers.impl.AbstractProvider#updateHost(org.netbeans.modules.php.rt.spi.providers.Host)
198: */
199: @Override
200: protected void updateHost(LocalHostImpl oldHost,
201: LocalHostImpl newHost) {
202: super .updateHost(oldHost, newHost);
203: }
204:
205: @Override
206: protected void serializeUpdated(LocalHostImpl oldHost,
207: LocalHostImpl newHost) {
208: FileObject dir = getHostsDir();
209: if (dir == null) {
210: return;
211: }
212:
213: String recordName = newHost.getName();
214:
215: String httpHostName = newHost.getDomain();
216: String httpPort = newHost.getPort();
217: String httpBaseDir = newHost.getBaseDirectory();
218:
219: String localPath = (String) newHost
220: .getProperty(LocalHostImpl.DOCUMENT_PATH);
221: String webServerConfig = (String) newHost
222: .getProperty(LocalHostImpl.WEB_CONFIG_FILE);
223: String phpConfig = (String) newHost
224: .getProperty(LocalHostImpl.PHP_CONFIG_FILE);
225:
226: FileObject hostObject = getHostObject(dir, oldHost);
227:
228: String fileName = FileUtil.findFreeFileName(dir, HOST, null);
229:
230: try {
231: if (hostObject == null) {
232: hostObject = dir.createData(fileName);
233: }
234:
235: hostObject.setAttribute(NAME, recordName);
236: hostObject.setAttribute(HOST_NAME, httpHostName);
237: hostObject.setAttribute(BASE_DIRECTORY_PATH, httpBaseDir);
238: hostObject.setAttribute(DOCUMENT_ROOT, localPath);
239: hostObject.setAttribute(WEB_SERVER_CONFIG_FILE,
240: webServerConfig);
241: hostObject.setAttribute(PHP_CONFIG_FILE, phpConfig);
242: if (httpPort != null
243: && !httpPort.equals(HostImpl.DEFAULT_PORT)) {
244: hostObject.setAttribute(PORT, httpPort);
245: }
246: } catch (IOException e) {
247: LOGGER.log(Level.WARNING, null, e);
248: }
249: }
250:
251: private boolean isLocalHostObject(Enumeration<String> attributes) {
252: if (attributes == null) {
253: return false;
254: }
255: List<String> list = Collections.list(attributes);
256: return list.contains(DOCUMENT_ROOT);
257: }
258:
259: private final LocalUiConfigProvider myConfig;
260:
261: private final LocalProjectConfigProvider myProjectProvider;
262:
263: private final LocalCommandProvider myCommandProvider;
264:
265: }
|