/**
 * Copyright 2004, 2005 The Apache Software Foundation or its licensors, as applicable
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.geronimo.st.core;

import java.io.IOException;
import java.net.URL;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jst.server.core.IWebModule;
import org.eclipse.jst.server.generic.core.internal.GenericServer;
import org.eclipse.jst.server.generic.core.internal.Trace;
import org.eclipse.wst.server.core.IModule;
import org.eclipse.wst.server.core.internal.ServerMonitorManager;

public abstract class GenericGeronimoServer extends GenericServer implements
		IGeronimoServer {

	public static final String PROPERTY_ADMIN_ID = "adminID";

	public static final String PROPERTY_ADMIN_PW = "adminPassword";

	public static final String PROPERTY_RMI_PORT = "rmiport";

	public static final String PROPERTY_HTTP_PORT = "port";

	public static final String PROPERTY_LOG_LEVEL = "logLevel";

	public static final String CONSOLE_INFO = "-v";

	public static final String CONSOLE_DEBUG = "-vv";

	public abstract String getContextRoot(IModule module);

	public URL getModuleRootURL(IModule module) {
		try {
			if (module == null
					|| module.loadAdapter(IWebModule.class, null) == null)
				return null;

			String host = getServer().getHost();
			String url = "http://" + host;
			int port = 0;

			port = getHttpPort();
			port = ServerMonitorManager.getInstance().getMonitoredPort(getServer(), port, "web");
			if (port != 80)
				url += ":" + port;

			String moduleId = getContextRoot(module);
			if (!moduleId.startsWith("/"))
				url += "/";
			url += moduleId;

			if (!url.endsWith("/"))
				url += "/";

			return new URL(url);
		} catch (Exception e) {
			Trace.trace("Could not get root URL", e);
			return null;
		}

	}

	public String getAdminID() {
		return (String) getServerInstanceProperties().get(PROPERTY_ADMIN_ID);
	}

	public String getAdminPassword() {
		return (String) getServerInstanceProperties().get(PROPERTY_ADMIN_PW);
	}

	public String getRMINamingPort() {
		return (String) getServerInstanceProperties().get(PROPERTY_RMI_PORT);
	}

	public String getHTTPPort() {
		return (String) getServerInstanceProperties().get(PROPERTY_HTTP_PORT);
	}

	public String getConsoleLogLevel() {
		return (String) getServerInstanceProperties().get(PROPERTY_LOG_LEVEL);
	}

	public void setAdminID(String value) {
		getServerInstanceProperties().put(PROPERTY_ADMIN_ID, value);
	}

	public void setAdminPassword(String value) {
		getServerInstanceProperties().put(PROPERTY_ADMIN_PW, value);
	}

	public void setRMINamingPort(String value) {
		getServerInstanceProperties().put(PROPERTY_RMI_PORT, value);
	}

	public void setHTTPPort(String value) {
		getServerInstanceProperties().put(PROPERTY_HTTP_PORT, value);
	}

	public void setConsoleLogLevel(String value) {
		getServerInstanceProperties().put(PROPERTY_LOG_LEVEL, value);
	}

	public String discoverDeploymentFactoryClassName(IPath jarPath) {
		try {
			JarFile deployerJar = new JarFile(jarPath.toFile());
			Manifest manifestFile = deployerJar.getManifest();
			Attributes attributes = manifestFile.getMainAttributes();
			return attributes.getValue("J2EE-DeploymentFactory-Implementation-Class");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jst.server.generic.core.internal.GenericServer#setDefaults(org.eclipse.core.runtime.IProgressMonitor)
	 */
	public void setDefaults(IProgressMonitor monitor) {
		setConsoleLogLevel(CONSOLE_INFO);
	}

}

@@ -741,16 +741,38 @@
 et.URL;%0A
+import java.util.Map;%0A
 import j
@@ -3069,33 +3069,27 @@
  value) %7B%0A%09%09
-g
+s
 et
-Server
 InstanceProp
@@ -3087,33 +3087,25 @@
 tancePropert
-ies().put
+y
 (PROPERTY_AD
@@ -3168,33 +3168,27 @@
  value) %7B%0A%09%09
-g
+s
 et
-Server
 InstanceProp
@@ -3186,33 +3186,25 @@
 tancePropert
-ies().put
+y
 (PROPERTY_AD
@@ -3267,33 +3267,27 @@
  value) %7B%0A%09%09
-g
+s
 et
-Server
 InstanceProp
@@ -3285,33 +3285,25 @@
 tancePropert
-ies().put
+y
 (PROPERTY_RM
@@ -3361,33 +3361,27 @@
  value) %7B%0A%09%09
-g
+s
 et
-Server
 InstanceProp
@@ -3379,33 +3379,25 @@
 tancePropert
-ies().put
+y
 (PROPERTY_HT
@@ -3467,25 +3467,19 @@
 ue) %7B%0A%09%09
-g
+s
 et
-Server
 Instance
@@ -3489,17 +3489,9 @@
 pert
-ies().put
+y
 (PRO
@@ -4156,12 +4156,183 @@
 NFO);%0A%09%7D
+%0A%09%0A%09private void setInstanceProperty(String name, String value) %7B%0A%09%09Map map = getServerInstanceProperties();%0A%09%09map.put(name, value);%0A%09%09setServerInstanceProperties(map);%0A%09%7D
 %0A%0A%7D%0A

fix regression, instance props not saving