Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2010 Sun Microsystems, Inc. |
41 |
*/ |
42 |
|
43 |
package org.netbeans.modules.ruby.railsprojects.server; |
44 |
|
45 |
import java.io.File; |
46 |
import java.util.ArrayList; |
47 |
import java.util.Collections; |
48 |
import java.util.List; |
49 |
import java.util.concurrent.Future; |
50 |
import java.util.regex.Pattern; |
51 |
import javax.swing.JComponent; |
52 |
import javax.swing.event.ChangeListener; |
53 |
import org.netbeans.api.ruby.platform.RubyPlatform; |
54 |
import org.netbeans.modules.ruby.platform.gems.GemInfo; |
55 |
import org.netbeans.modules.ruby.railsprojects.RailsProjectUtil.RailsVersion; |
56 |
import org.netbeans.modules.ruby.railsprojects.server.nodes.RubyServerNode; |
57 |
import org.netbeans.spi.server.ServerInstanceImplementation; |
58 |
import org.openide.nodes.Node; |
59 |
import org.openide.util.ChangeSupport; |
60 |
import org.openide.util.NbBundle; |
61 |
import org.openide.util.Parameters; |
62 |
|
63 |
/** |
64 |
* |
65 |
* @author David Calavera |
66 |
*/ |
67 |
public abstract class JRubyServerBase implements RubyServer, ServerInstanceImplementation { |
68 |
|
69 |
private final List<RailsApplication> applications = new ArrayList<RailsApplication>(); |
70 |
private final RubyPlatform platform; |
71 |
private final String version; |
72 |
private final String location; |
73 |
private final ChangeSupport changeSupport = new ChangeSupport(this); |
74 |
private Node node; |
75 |
|
76 |
/* abstract methods */ |
77 |
protected abstract String getLabel(); |
78 |
protected abstract Pattern[] getPatterns(); |
79 |
protected abstract String getGemName(); |
80 |
|
81 |
JRubyServerBase(RubyPlatform platform, GemInfo gemInfo) { |
82 |
Parameters.notNull("platform", platform); //NOI18N |
83 |
this.platform = platform; |
84 |
this.version = gemInfo.getVersion(); |
85 |
this.location = getGemFolder(gemInfo.getSpecFile()); |
86 |
} |
87 |
|
88 |
private String getGemFolder(File specFile) { |
89 |
String gemFolderName = specFile.getName(); |
90 |
if(gemFolderName.endsWith(".gemspec")) { |
91 |
gemFolderName = gemFolderName.substring(0, gemFolderName.length() - 8); |
92 |
} |
93 |
|
94 |
return new File(specFile.getParentFile().getParentFile(), |
95 |
"gems" + File.separatorChar + gemFolderName).getAbsolutePath(); |
96 |
} |
97 |
|
98 |
private Node getNode() { |
99 |
if (this.node == null) { |
100 |
this.node = new RubyServerNode(this); |
101 |
} |
102 |
return node; |
103 |
} |
104 |
|
105 |
@Override |
106 |
public String getNodeName() { |
107 |
return NbBundle.getMessage(getClass(), "LBL_ServerNodeName", getDisplayName(), platform.getLabel()); |
108 |
} |
109 |
|
110 |
@Override |
111 |
public String getLocation() { |
112 |
return location; |
113 |
} |
114 |
|
115 |
@Override |
116 |
public List<String> getStartupParams(RailsVersion version) { |
117 |
return Collections.emptyList(); |
118 |
} |
119 |
|
120 |
@Override |
121 |
public String getScriptPrefix() { |
122 |
return "-S"; |
123 |
} |
124 |
|
125 |
@Override |
126 |
public String getServerPath(RailsVersion version) { |
127 |
return getGemName().toLowerCase(); |
128 |
} |
129 |
|
130 |
public boolean isStartupMsg(String outputLine) { |
131 |
for (Pattern each : getPatterns()) { |
132 |
if (each.matcher(outputLine).matches()) { |
133 |
return true; |
134 |
} |
135 |
} |
136 |
return false; |
137 |
} |
138 |
|
139 |
@Override |
140 |
public List<RailsApplication> getApplications() { |
141 |
return Collections.unmodifiableList(applications); |
142 |
} |
143 |
|
144 |
@Override |
145 |
public boolean addApplication(RailsApplication application) { |
146 |
boolean result = applications.add(application); |
147 |
changeSupport.fireChange(); |
148 |
return result; |
149 |
} |
150 |
|
151 |
@Override |
152 |
public boolean removeApplication(int port) { |
153 |
boolean result = false; |
154 |
for (RailsApplication app : applications) { |
155 |
if (app.getPort() == port) { |
156 |
result = applications.remove(app); |
157 |
changeSupport.fireChange(); |
158 |
break; |
159 |
} |
160 |
} |
161 |
|
162 |
return result; |
163 |
} |
164 |
|
165 |
public void addChangeListener(ChangeListener listener) { |
166 |
changeSupport.addChangeListener(listener); |
167 |
} |
168 |
|
169 |
public void removeChangeListener(ChangeListener listener) { |
170 |
changeSupport.removeChangeListener(listener); |
171 |
} |
172 |
|
173 |
public int compareVersion(String targetVersion) { |
174 |
int [] sv = extractVersion(version); |
175 |
int [] tv = extractVersion(targetVersion); |
176 |
for(int i = 0; i < 3; i++) { |
177 |
if(sv[i] < tv[i]) { |
178 |
return -1; |
179 |
} else if(sv[i] > tv[i]) { |
180 |
return 1; |
181 |
} |
182 |
} |
183 |
return 0; |
184 |
} |
185 |
|
186 |
private int [] extractVersion(String vs) { |
187 |
int [] v = new int [] { 0, 0, 0 }; |
188 |
String [] parts = vs.split("\\."); |
189 |
for(int i = 0; i < 3 && i < parts.length; i++) { |
190 |
v[i] = Integer.valueOf(parts[i]); |
191 |
} |
192 |
return v; |
193 |
} |
194 |
|
195 |
@Override |
196 |
public String getServerUri() { |
197 |
return getGemName().toUpperCase(); |
198 |
} |
199 |
|
200 |
@Override |
201 |
public String getDisplayName() { |
202 |
return NbBundle.getMessage(getClass(), getLabel(), version); |
203 |
} |
204 |
|
205 |
@Override |
206 |
public ServerState getServerState() { |
207 |
return null; |
208 |
} |
209 |
|
210 |
@Override |
211 |
public Future<OperationState> startServer(RubyPlatform platform) { |
212 |
throw new UnsupportedOperationException("Not supported yet."); |
213 |
} |
214 |
|
215 |
@Override |
216 |
public Future<OperationState> stopServer() { |
217 |
throw new UnsupportedOperationException("Not supported yet."); |
218 |
} |
219 |
|
220 |
@Override |
221 |
public Future<OperationState> deploy(String applicationName, File applicationDir) { |
222 |
throw new UnsupportedOperationException("Not supported yet."); |
223 |
} |
224 |
|
225 |
@Override |
226 |
public Future<OperationState> stop(String applicationName) { |
227 |
throw new UnsupportedOperationException("Not supported yet."); |
228 |
} |
229 |
|
230 |
@Override |
231 |
public Future<OperationState> runApplication(RubyPlatform platform, String applicationName, File applicationDir) { |
232 |
throw new UnsupportedOperationException("Not supported yet."); |
233 |
} |
234 |
|
235 |
@Override |
236 |
public boolean isPlatformSupported(RubyPlatform platform) { |
237 |
return this.platform.equals(platform); |
238 |
} |
239 |
|
240 |
@Override |
241 |
public String getContextRoot(String applicationName) { |
242 |
return ""; |
243 |
} |
244 |
|
245 |
@Override |
246 |
public int getRailsPort() { |
247 |
return 3000; |
248 |
} |
249 |
|
250 |
@Override |
251 |
public String getServerCommand(RubyPlatform platform, String classpath, File applicationDir, int httpPort, boolean debug) { |
252 |
throw new UnsupportedOperationException("Not supported yet."); |
253 |
} |
254 |
|
255 |
@Override |
256 |
public String getServerDisplayName() { |
257 |
return getNodeName(); |
258 |
} |
259 |
|
260 |
@Override |
261 |
public Node getFullNode() { |
262 |
return getNode(); |
263 |
} |
264 |
|
265 |
@Override |
266 |
public Node getBasicNode() { |
267 |
return getNode(); |
268 |
} |
269 |
|
270 |
@Override |
271 |
public JComponent getCustomizer() { |
272 |
return null; |
273 |
} |
274 |
|
275 |
@Override |
276 |
public void remove() { |
277 |
throw new UnsupportedOperationException("Not supported yet."); |
278 |
} |
279 |
|
280 |
@Override |
281 |
public boolean isRemovable() { |
282 |
return false; |
283 |
} |
284 |
|
285 |
@Override |
286 |
public boolean equals(Object obj) { |
287 |
if (obj == null) { |
288 |
return false; |
289 |
} |
290 |
if (getClass() != obj.getClass()) { |
291 |
return false; |
292 |
} |
293 |
final JRubyServerBase other = (JRubyServerBase) obj; |
294 |
if (this.platform != other.platform && (this.platform == null || !this.platform.equals(other.platform))) { |
295 |
return false; |
296 |
} |
297 |
if (this.version != other.version && (this.version == null || !this.version.equals(other.version))) { |
298 |
return false; |
299 |
} |
300 |
if (this.location != other.location && (this.location == null || !this.location.equals(other.location))) { |
301 |
return false; |
302 |
} |
303 |
return true; |
304 |
} |
305 |
|
306 |
@Override |
307 |
public int hashCode() { |
308 |
int hash = 3; |
309 |
hash = 47 * hash + (this.platform != null ? this.platform.hashCode() : 0); |
310 |
hash = 47 * hash + (this.version != null ? this.version.hashCode() : 0); |
311 |
return hash; |
312 |
} |
313 |
} |