/*
* Created on Dec 16, 2004
*
* Copyright 2005 CafeSip.org
*
* 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.cafesip.jiplet.console.server;
import java.io.File;
import java.net.URL;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import org.cafesip.jiplet.jmxbeans.ContextElement;
import org.cafesip.jiplet.jmxbeans.JipletElement;
import org.cafesip.jiplet.utils.FileUtils;
/**
* @author amit
*
*/
public class ContainerMgmt
{
/**
*
*/
public ContainerMgmt()
{
}
public ContextElement getContextProperty(String name) throws Exception
{
AgentConnectionPool pool = Model.getJmxConnectionPool();
MBeanServerConnection server = null;
try
{
server = pool.getConnection();
if (server == null)
{
String err = "Error obtaining JMX connection. Is the server running?";
throw new Exception(err);
}
Object[] args = new Object[] { name };
String[] sig = new String[] { "java.lang.String" };
ContextElement context = (ContextElement) server.invoke(
new ObjectName("org.cafesip.jiplet:type=JipletContainer"),
"getContextProperty", args, sig);
if (context == null)
{
throw new Exception("Context '" + name + "' not found");
}
return context;
}
finally
{
if (server != null)
{
pool.returnConnection(server);
}
}
}
public org.cafesip.jiplet.console.client.rpc.ContextElement viewContextProperty(
String name) throws Exception
{
ContextElement src = getContextProperty(name);
org.cafesip.jiplet.console.client.rpc.ContextElement dest = new org.cafesip.jiplet.console.client.rpc.ContextElement();
dest.setDisplayName(src.getDisplayName());
dest.setName(src.getName());
dest.setPath(src.getPath());
dest.setSelectionCriteriaStringList(src.getSelectionCriteria());
dest.setTypeDescription(src.getTypeDescription());
dest
.setJiplets(new org.cafesip.jiplet.console.client.rpc.JipletElement[0]);
org.cafesip.jiplet.console.client.rpc.JipletElement[] jiplets = listJipletsProperties(name);
dest.setJiplets(jiplets);
return dest;
}
public String[] listContexts() throws Exception
{
AgentConnectionPool pool = Model.getJmxConnectionPool();;
MBeanServerConnection server = null;
try
{
server = pool.getConnection();
if (server == null)
{
String err = "Error obtaining JMX connection. Is the server running?";
throw new Exception(err);
}
return (String[]) server.invoke(new ObjectName(
"org.cafesip.jiplet:type=JipletContainer"), "listContexts",
null, null);
}
finally
{
if (server != null)
{
pool.returnConnection(server);
}
}
}
public org.cafesip.jiplet.console.client.rpc.ContextElement[] listContextsProperties()
throws Exception
{
String[] contexts = listContexts();
org.cafesip.jiplet.console.client.rpc.ContextElement[] elements = new org.cafesip.jiplet.console.client.rpc.ContextElement[contexts.length];
for (int i = 0; i < contexts.length; i++)
{
elements[i] = viewContextProperty(contexts[i]);
}
return elements;
}
public void createContext(String path, String overriddenName, String mapping)
throws Exception
{
AgentConnectionPool pool = Model.getJmxConnectionPool();;
MBeanServerConnection server = null;
try
{
server = pool.getConnection();
if (server == null)
{
String err = "Error obtaining JMX connection. Is the server running?";
throw new Exception(err);
}
File from_path = new File(path);
String name = from_path.getName();
String context_name = name;
if (name.endsWith(FileUtils.SPR_EXTENSION) == true)
{
context_name = name.substring(0, name.length()
- FileUtils.SPR_EXTENSION.length());
}
if (mapping != null)
{
Object[] args = new Object[] { context_name, mapping };
String[] sig = new String[] { "java.lang.String",
"java.lang.String" };
String errorMessage = (String) server.invoke(new ObjectName(
"org.cafesip.jiplet:type=JipletContainer"),
"addContextMapping", args, sig);
if (errorMessage.length() > 0)
{
throw new Exception("Error adding context mapping: "
+ errorMessage);
}
}
if (Model.getDeploymentType().equals("JBOSS") == true)
{
// copy the spr file/directory into the JBOSS deployment
// directory
String home = System.getProperty("jboss.server.home.url");
URL home_url = new URL(home);
File home_path = new File(home_url.getPath(), "deploy");
FileUtils f = new FileUtils();
if (overriddenName != null)
{
String path_name = null;
if (name.endsWith(FileUtils.SPR_EXTENSION) == true)
{
path_name = (new File(home_path, overriddenName
+ FileUtils.SPR_EXTENSION)).getAbsolutePath();
}
else
{
path_name = (new File(home_path, overriddenName))
.getAbsolutePath();
}
// System.out.println("(O) Copying from " + path + " to " +
// path_name);
if (f.copyFile(path, path_name) == false)
{
throw new Exception(
"Error copying file to the J2EE deploy directory");
}
}
else
{
// System.out.println("Copying from " + path + " to " +
// home_path.getAbsolutePath());
if (f.copy(path, home_path.getAbsolutePath()) == false)
{
throw new Exception(
"Error copying file to the J2EE deploy directory");
}
}
}
else
// servlet container (use the JMX interface)
{
Object[] args = new Object[] { path, overriddenName };
String[] sig = new String[] { "java.lang.String",
"java.lang.String" };
String errorMessage = (String) server.invoke(new ObjectName(
"org.cafesip.jiplet:type=JipletContainer"),
"createContext", args, sig);
if (errorMessage.length() > 0)
{
throw new Exception(errorMessage);
}
}
}
finally
{
if (server != null)
{
pool.returnConnection(server);
}
}
}
public void deleteContext(String name) throws Exception
{
AgentConnectionPool pool =Model.getJmxConnectionPool();;
MBeanServerConnection server = null;
try
{
server = pool.getConnection();
if (server == null)
{
String err = "Error obtaining JMX connection. Is the server running?";
throw new Exception(err);
}
Object[] args = new Object[] { name };
String[] sig = new String[] { "java.lang.String" };
String errorMessage = (String) server.invoke(new ObjectName(
"org.cafesip.jiplet:type=JipletContainer"),
"removeContextMapping", args, sig);
if (errorMessage.length() > 0)
{
throw new Exception(errorMessage);
}
if (Model.getDeploymentType().equals("JBOSS") == true)
{
String home = System.getProperty("jboss.server.home.url");
URL home_url = new URL(home);
File f = null;
if (name.endsWith(FileUtils.SPR_EXTENSION))
{
f = new File(home_url.getPath() + "/deploy/" + name);
}
else
{
f = new File(home_url.getPath() + "/deploy/" + name
+ FileUtils.SPR_EXTENSION);
}
if (f.isDirectory() == true)
{
FileUtils futils = new FileUtils();
if (futils.deleteDir(f.getAbsolutePath()) == false)
{
throw new Exception(
"Error deleting J2EE deploy directory");
}
}
else
{
if (f.delete() == false)
{
throw new Exception(
"Error deleting spr file from the J2EE deploy directory");
}
}
Thread.sleep(10000L);
}
else
{
args = new Object[] { name };
sig = new String[] { "java.lang.String" };
errorMessage = (String) server.invoke(new ObjectName(
"org.cafesip.jiplet:type=JipletContainer"),
"deleteContext", args, sig);
if (errorMessage.length() > 0)
{
throw new Exception(errorMessage);
}
}
}
finally
{
if (server != null)
{
pool.returnConnection(server);
}
}
}
public String getDeploymentDir() throws Exception
{
AgentConnectionPool pool = Model.getJmxConnectionPool();;
MBeanServerConnection server = null;
try
{
server = pool.getConnection();
if (server == null)
{
String err = "Error obtaining JMX connection. Is the server running?";
throw new Exception(err);
}
String name = (String) server.getAttribute(new ObjectName(
"org.cafesip.jiplet:type=JipletContainer"), "DeployDir");
if (name == null)
{
throw new Exception("Call to getDeployDir returned null");
}
if (name.length() == 0)
{
throw new Exception(
"Call to getDeployDir returned empty string");
}
return name;
}
finally
{
if (server != null)
{
pool.returnConnection(server);
}
}
}
public String getDefaultConnectorName() throws Exception
{
AgentConnectionPool pool = Model.getJmxConnectionPool();;
MBeanServerConnection server = null;
try
{
server = pool.getConnection();
if (server == null)
{
String err = "Error obtaining JMX connection. Is the server running?";
throw new Exception(err);
}
String name = (String) server.getAttribute(new ObjectName(
"org.cafesip.jiplet:type=JipletContainer"),
"DefaultConnectorName");
if (name == null)
{
throw new Exception(
"Call to getDefaultConnectorName returned null");
}
if (name.length() == 0)
{
throw new Exception(
"Call to getDefaultConnectorName returned empty string");
}
return name;
}
finally
{
if (server != null)
{
pool.returnConnection(server);
}
}
}
public String[] listJiplets(String contextName) throws Exception
{
AgentConnectionPool pool = Model.getJmxConnectionPool();;
MBeanServerConnection server = null;
try
{
server = pool.getConnection();
if (server == null)
{
String err = "Error obtaining JMX connection. Is the server running?";
throw new Exception(err);
}
return (String[]) server.invoke(
new ObjectName(
"org.cafesip.jiplet:type=JipletContext,name="
+ contextName), "listJiplets", null, null);
}
finally
{
if (server != null)
{
pool.returnConnection(server);
}
}
}
public org.cafesip.jiplet.console.client.rpc.JipletElement[] listJipletsProperties(
String contextName) throws Exception
{
String[] jiplets = listJiplets(contextName);
org.cafesip.jiplet.console.client.rpc.JipletElement[] list = new org.cafesip.jiplet.console.client.rpc.JipletElement[jiplets.length];
for (int i = 0; i < jiplets.length; i++)
{
list[i] = viewJipletProperty(contextName, jiplets[i]);
}
return list;
}
public JipletElement getJipletProperty(String contextName, String jipletName)
throws Exception
{
AgentConnectionPool pool = Model.getJmxConnectionPool();
MBeanServerConnection server = null;
try
{
server = pool.getConnection();
if (server == null)
{
String err = "Error obtaining JMX connection. Is the server running?";
throw new Exception(err);
}
Object[] args = new Object[] { jipletName };
String[] sig = new String[] { "java.lang.String" };
JipletElement jiplet = (JipletElement) server.invoke(
new ObjectName(
"org.cafesip.jiplet:type=JipletContext,name="
+ contextName), "getJipletProperty", args,
sig);
if (jiplet == null)
{
throw new Exception("Jiplet not found");
}
return jiplet;
}
finally
{
if (server != null)
{
pool.returnConnection(server);
}
}
}
public org.cafesip.jiplet.console.client.rpc.JipletElement viewJipletProperty(
String contextName, String jipletName) throws Exception
{
JipletElement src = getJipletProperty(contextName, jipletName);
org.cafesip.jiplet.console.client.rpc.JipletElement dest = new org.cafesip.jiplet.console.client.rpc.JipletElement();
dest.setConnectorName(src.getConnectorName());
dest.setDescription(src.getDescription());
dest.setInitParamsMap(src.getInitParams());
dest.setName(src.getName());
dest.setParentContext(src.getParentContext());
dest.setSecurityConstraint(src.getSecurityConstraint());
dest.setSelectionCriteriaStringList(src.getSelectionCriteria());
return dest;
}
}