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: package org.netbeans.modules.php.rt.providers.impl;
042:
043: import java.net.MalformedURLException;
044: import java.net.URL;
045: import org.netbeans.modules.php.rt.spi.providers.Host;
046: import org.openide.util.NbBundle;
047:
048: /**
049: * @author ads
050: *
051: */
052: public abstract class HostImpl implements Host {
053:
054: public static final String DEFAULT_PORT = "80"; // NOI18N
055: // server record name
056:
057: public static final String NAME = "name"; // NOI18N
058: // http server name
059:
060: public static final String DOMAIN = "domain"; // NOI18N
061: // http server port
062:
063: public static final String PORT = "port"; // NOI18N
064: // base orl of http server
065:
066: public static final String BASE_DIRECTORY_PATH = "base-directory-path"; // NOI18N
067:
068: private static final String MSG_NOT_CONFIGURED_HTTP = "MSG_NotConfiguredHttp"; // NOI18N
069:
070: public HostImpl(String name, AbstractProvider provider) {
071: this (name, null, null, null, provider);
072: }
073:
074: public HostImpl(String name, String domain, String port,
075: String baseDir, AbstractProvider provider) {
076: assert name != null;
077: myName = name;
078: setProperty(DOMAIN, domain);
079: setProperty(PORT, port);
080: setProperty(BASE_DIRECTORY_PATH, baseDir);
081: myProvider = provider;
082: }
083:
084: /* (non-Javadoc)
085: * @see org.netbeans.modules.php.rt.spi.providers.Host#getProvider()
086: */
087: public AbstractProvider getProvider() {
088: return myProvider;
089: }
090:
091: /* (non-Javadoc)
092: * @see java.lang.Object#equals(java.lang.Object)
093: */
094: @Override
095: public boolean equals(Object obj) {
096: if (!(obj instanceof HostImpl)) {
097: return false;
098: }
099: HostImpl impl = (HostImpl) obj;
100: boolean idEq = getName().equals(impl.getName());
101: if (!idEq) {
102: return false;
103: }
104: /*
105: boolean domainEq = getDomain().equals( impl.getDomain() );
106: if ( !domainEq ){
107: return false;
108: }
109: return getPort().equals( impl.getPort() );
110: */
111: return true;
112: }
113:
114: /* (non-Javadoc)
115: * @see java.lang.Object#hashCode()
116: */
117: @Override
118: public int hashCode() {
119: return getName().hashCode();
120: //return getDomain().hashCode()*37 + getPort().hashCode();
121: }
122:
123: /* (non-Javadoc)
124: * @see org.netbeans.modules.php.rt.spi.providers.Host#getProperty(java.lang.String)
125: */
126: public Object getProperty(String key) {
127: if (NAME.equals(key)) {
128: return myName;
129: } else if (PORT.equals(key)) {
130: return myPort;
131: } else if (DOMAIN.equals(key)) {
132: return myDomain;
133: } else if (BASE_DIRECTORY_PATH.equals(key)) {
134: return myBaseDirectory;
135: } else {
136: return null;
137: }
138: }
139:
140: /* (non-Javadoc)
141: * @see org.netbeans.modules.php.rt.spi.providers.Host#putProperty(java.lang.String, java.lang.Object)
142: */
143: public void setProperty(String key, Object value) {
144: /*
145: * Prevent resetting main properties of this class.
146: * One need to reisntatiate class instead of changing these props.
147: */
148: assert !NAME.equals(key) : "Property with key " + key + // NOI18N
149: " is not mutable. You need to reinstatiate this class"; // NOI18N
150: if (DOMAIN.equals(key)) {
151: myDomain = toStringValue(value);
152: } else if (PORT.equals(key)) {
153: myPort = toStringValue(value);
154: } else if (BASE_DIRECTORY_PATH.equals(key)) {
155: myBaseDirectory = toStringValue(value);
156: }
157: }
158:
159: public void remove() {
160: AbstractProvider provider = getProvider();
161: provider.remove(this );
162: }
163:
164: /* (non-Javadoc)
165: * @see java.lang.Object#toString()
166: */
167: @Override
168: public String toString() {
169: return getDisplayName();
170: }
171:
172: public String getId() {
173: return getName();
174: }
175:
176: public String getDisplayName() {
177: return getName();
178: }
179:
180: public String getServerName() {
181: if (getPort().equals(DEFAULT_PORT)) {
182: return getDomain();
183: } else {
184: return getDomain() + ":" + getPort(); // NOI18N
185: }
186: }
187:
188: protected String toStringValue(Object value) {
189: if (value == null) {
190: return "";
191: }
192: assert value instanceof String;
193: return (String) value;
194: }
195:
196: protected char[] toCharArrValue(Object value) {
197: if (value == null) {
198: return "".toCharArray();
199: }
200: assert value instanceof char[];
201: return (char[]) value;
202: }
203:
204: protected String getName() {
205: return myName;
206: }
207:
208: protected String getDomain() {
209: return myDomain;
210: }
211:
212: protected String getPort() {
213: if (myPort == null || myPort.trim().length() == 0) {
214: return DEFAULT_PORT;
215: }
216: return myPort;
217: }
218:
219: protected String getBaseDirectory() {
220: return myBaseDirectory;
221: }
222:
223: private String myName;
224: private String myDomain;
225: private String myPort;
226: private String myBaseDirectory;
227: private AbstractProvider myProvider;
228:
229: /**
230: * contains Host helper methods
231: */
232: public static class Helper {
233:
234: public final static String HTTP = "http";
235:
236: public static String noHttpMessage() {
237: return NbBundle.getMessage(HostImpl.class,
238: MSG_NOT_CONFIGURED_HTTP);
239: //return HTTP+"://";
240: }
241:
242: public static boolean isHttpReady(HostImpl host) {
243: String domain = host.getDomain();
244: if (domain == null || domain.trim().length() == 0) {
245: return false;
246: }
247: return true;
248: }
249:
250: public static String getHttpUrl(HostImpl host) {
251: if (!isHttpReady(host)) {
252: return null;
253: }
254:
255: String httpUrlResult = null;
256:
257: String domain = host.getDomain();
258: int port = Integer.parseInt(host.getPort());
259: String baseDirectoryPath = (String) host
260: .getProperty(BASE_DIRECTORY_PATH);
261:
262: if (baseDirectoryPath != null
263: && !baseDirectoryPath.startsWith("/")) {
264: baseDirectoryPath = "/" + baseDirectoryPath;
265: }
266:
267: URL httpUrl = null;
268: try {
269: httpUrl = new URL(HTTP, domain, port, baseDirectoryPath);
270: httpUrlResult = httpUrl.toString();
271: } catch (MalformedURLException ex) {
272: httpUrlResult = null;
273: } catch (Exception e) {
274: httpUrlResult = null;
275: }
276: return httpUrlResult;
277: }
278:
279: /**
280: * joins base url with additional subdirectory path.
281: * e.g.
282: * <pre>
283: * 'http://server/project' with 'subproject'
284: * </pre>
285: * result is
286: * <pre>
287: * 'http://server/project/subproject'
288: * </pre>
289: */
290: public static String addSubdirectoryToUrl(String baseUrl,
291: String subdir) throws MalformedURLException {
292: if (subdir == null) {
293: return baseUrl;
294: }
295:
296: String context = subdir;
297: if (!context.startsWith("/")) {
298: context = "/" + context;
299: }
300:
301: URL rootUrl = new URL(baseUrl);
302: String rootPath = rootUrl.getPath();
303: // prevent double slash on rootPath and context connection
304: if (rootPath.endsWith("/") && context.startsWith("/")) {
305: rootPath = rootPath.substring(0, rootPath.length() - 1);
306: }
307:
308: URL resultUrl = new URL(rootUrl, rootPath + context);
309:
310: return resultUrl.toString();
311: }
312: }
313: }
|