blob: 737b9900cf5b995a60721c9abb15eaf0bacbff5e [file] [log] [blame]
deboer89593392007-05-09 15:56:40 +00001/*******************************************************************************
deboer30ba5222008-01-08 19:50:04 +00002 * Copyright (c) 2007, 2008 IBM Corporation and others.
deboer89593392007-05-09 15:56:40 +00003 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - Initial API and implementation
10 *******************************************************************************/
deboer9d04b8d2007-05-09 22:01:05 +000011package org.eclipse.wst.server.preview.adapter.internal.core;
deboer89593392007-05-09 15:56:40 +000012
13import java.net.URL;
14
15import org.eclipse.core.runtime.CoreException;
16import org.eclipse.core.runtime.IProgressMonitor;
17import org.eclipse.core.runtime.IStatus;
18import org.eclipse.core.runtime.NullProgressMonitor;
19import org.eclipse.core.runtime.Status;
20import org.eclipse.wst.server.core.IModule;
21import org.eclipse.wst.server.core.IRuntime;
22import org.eclipse.wst.server.core.IRuntimeType;
23import org.eclipse.wst.server.core.IRuntimeWorkingCopy;
24import org.eclipse.wst.server.core.IServer;
25import org.eclipse.wst.server.core.IServerType;
26import org.eclipse.wst.server.core.IServerWorkingCopy;
27import org.eclipse.wst.server.core.ServerCore;
28import org.eclipse.wst.server.core.ServerPort;
29import org.eclipse.wst.server.core.model.IURLProvider;
30import org.eclipse.wst.server.core.model.ServerDelegate;
31import org.eclipse.wst.server.core.util.IStaticWeb;
32/**
33 * HTTP preview server.
34 */
35public class PreviewServer extends ServerDelegate implements IURLProvider {
36 public static final String ID = "org.eclipse.wst.server.preview.server";
37
38 public static final String PROPERTY_PORT = "port";
39
40 /**
41 * PreviewServer.
42 */
43 public PreviewServer() {
44 super();
45 }
46
47 protected void initialize() {
48 // do nothing
49 }
50
51 public PreviewRuntime getPreviewRuntime() {
52 if (getServer().getRuntime() == null)
53 return null;
54
55 return (PreviewRuntime) getServer().getRuntime().loadAdapter(PreviewRuntime.class, null);
56 }
57
58 /**
59 * Return the root URL of this module.
60 *
61 * @param module a module
62 * @return java.net.URL
63 */
64 public URL getModuleRootURL(IModule module) {
65 try {
66 String base = "http://localhost";
67
68 int port = getPort();
69 URL url = null;
70 if (port == 80)
71 url = new URL(base + "/");
72 else
73 url = new URL(base + ":" + port + "/");
74
75 String type = module.getModuleType().getId();
76 if ("wst.web".equals(type)) {
77 IStaticWeb staticWeb = (IStaticWeb) module.loadAdapter(IStaticWeb.class, null);
78 return new URL(url, staticWeb.getContextRoot());
79 }
80 return url;
81 } catch (Exception e) {
82 Trace.trace(Trace.SEVERE, "Could not get root URL", e);
83 return null;
84 }
85 }
86
87 /*
88 * Returns the child module(s) of this module.
89 */
90 public IModule[] getChildModules(IModule[] module) {
91 return new IModule[0];
92 }
93
94 /*
95 * Returns the root module(s) of this module.
96 */
97 public IModule[] getRootModules(IModule module) throws CoreException {
98 return new IModule[] { module };
99 }
100
101 /**
102 * Returns true if the given project is supported by this server, and false
103 * otherwise.
104 *
105 * @param add modules
106 * @param remove modules
107 * @return the status
108 */
109 public IStatus canModifyModules(IModule[] add, IModule[] remove) {
110 return new Status(IStatus.OK, PreviewPlugin.PLUGIN_ID, 0, Messages.canModifyModules, null);
111 }
112
113 public ServerPort[] getServerPorts() {
114 int port = getPort();
115 ServerPort[] ports = { new ServerPort("http", Messages.httpPort, port, "http") };
116 return ports;
117 }
118
119 public int getPort() {
120 return getAttribute(PreviewServer.PROPERTY_PORT, 8080);
121 }
122
123 public void setPort(int port) {
124 setAttribute(PreviewServer.PROPERTY_PORT, port);
125 }
126
127 public static IServer createPreviewServer(String serverName) {
128 try {
129 NullProgressMonitor monitor = new NullProgressMonitor();
130 IRuntimeType runtimeType = ServerCore.findRuntimeType(PreviewRuntime.ID);
131 IRuntimeWorkingCopy runtimeCopy = runtimeType.createRuntime(PreviewRuntime.ID, monitor);
132 IRuntime runtime = runtimeCopy.save(true, monitor);
133
134 IServerType serverType = ServerCore.findServerType(ID);
135 IServerWorkingCopy workingCopy = serverType.createServer(ID, null, runtime, monitor);
136 workingCopy.setName(serverName);
137 workingCopy.setHost("localhost");
138 return workingCopy.save(true, monitor);
139 } catch (Exception e) {
140 Trace.trace(Trace.SEVERE, "Error creating server", e);
141 }
142
143 return null;
144 }
145
146 public static IServer findPreviewServer(String id) {
147 IServer[] servers = ServerCore.getServers();
deboer30ba5222008-01-08 19:50:04 +0000148 for (IServer server : servers) {
149 if (server.getId().equals(id))
150 return server;
deboer89593392007-05-09 15:56:40 +0000151 }
152 return null;
153 }
154
155 public void modifyModules(IModule[] add, IModule[] remove, IProgressMonitor monitor) throws CoreException {
156 // do nothing
157 }
158
159 /**
160 * Return a string representation of this object.
161 *
162 * @return java.lang.String
163 */
164 public String toString() {
165 return "PreviewServer";
166 }
167}