/*
* Created on Nov 29, 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.jboss;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.cafesip.jiplet.JipletContainer;
import org.cafesip.jiplet.JipletException;
import org.cafesip.jiplet.JipletLogger;
import org.cafesip.jiplet.utils.FileUtils;
import org.jboss.deployment.MainDeployer;
import org.jboss.system.ServiceMBeanSupport;
/**
* @author amit
*
*/
public class JipletService extends ServiceMBeanSupport implements
JipletServiceMBean
{
private File file;
private SprDeployer sprDeployer;
private SrrDeployer srrDeployer;
/**
* @throws JipletException
* @throws IOException
*
*/
public JipletService() throws JipletException, IOException
{
super();
String home = System.getProperty("jboss.server.home.url");
URL home_url = new URL(home);
file = new File(home_url.getPath(), "deploy/jiplet-service.sar");
if (file.exists() == false)
{
throw new JipletException("The jiplet SAR was not found");
}
// define the logger
JBossLogger logger = new JBossLogger(log);
JipletLogger.configLogger(logger);
// define the config directory
String jboss_conf = System.getProperty("jboss.server.config.url");
URL conf_url = new URL(jboss_conf);
File conf_dir = new File(conf_url.getPath(), "jiplet");
if (conf_dir.exists() == false)
{
if (conf_dir.mkdir() == false)
{
throw new JipletException(
"Could not create configuration directory "
+ conf_dir.getAbsolutePath());
}
}
copyConfigFiles(file, conf_dir);
JipletContainer.setConfDir(conf_dir);
JipletContainer.setVendorDeploymentFactory(new JBossDeploymentDescriptorFactory());
// define the deploy directory
File deploy_dir = new File(home_url.getPath(), "jiplet-work");
if (deploy_dir.exists() == false)
{
if (deploy_dir.mkdir() == false)
{
throw new JipletException ("Could not create jiplet deployment directory "
+ deploy_dir.getAbsolutePath());
}
}
FileUtils fu = new FileUtils();
fu.cleanupDir(deploy_dir.getAbsolutePath());
JipletContainer.setDeployDir(deploy_dir);
}
/*
* @see org.jboss.system.ServiceMBean#getName()
*/
public String getName()
{
return "Jiplet container service";
}
/*
* @see org.jboss.system.ServiceMBeanSupport#startService()
*/
protected void startService() throws Exception
{
JipletContainer.setJmxAgent(server);
super.startService();
JipletContainer.getInstance().init();
registerDeployers();
}
/*
* @see org.jboss.system.ServiceMBeanSupport#stopService()
*/
protected void stopService() throws Exception
{
super.stopService();
unregisterDeployers();
JipletContainer.getInstance().destroy();
}
private void registerDeployers()
{
try
{
sprDeployer = new SprDeployer();
server.registerMBean(sprDeployer, SprDeployer.OBJECT_NAME);
server.invoke(MainDeployer.OBJECT_NAME, "addDeployer",
new Object[] { sprDeployer },
new String[] { "org.jboss.deployment.SubDeployer" });
srrDeployer = new SrrDeployer();
server.registerMBean(srrDeployer, SrrDeployer.OBJECT_NAME);
server.invoke(MainDeployer.OBJECT_NAME, "addDeployer",
new Object[] { srrDeployer },
new String[] { "org.jboss.deployment.SubDeployer" });
}
catch (Exception e)
{
JipletLogger
.error("Could not deploy SprDeployer - spr/srr deployment feature will not work.\n"
+ e.getClass().getName() + ": " + e.getMessage());
}
}
private void unregisterDeployers()
{
try
{
server.invoke(MainDeployer.OBJECT_NAME, "removeDeployer",
new Object[] { sprDeployer },
new String[] { "org.jboss.deployment.SubDeployer" });
server.unregisterMBean(SprDeployer.OBJECT_NAME);
server.invoke(MainDeployer.OBJECT_NAME, "removeDeployer",
new Object[] { srrDeployer },
new String[] { "org.jboss.deployment.SubDeployer" });
server.unregisterMBean(SrrDeployer.OBJECT_NAME);
}
catch (Exception e)
{
JipletLogger
.error("Could not undeploy SprDeployer - spr/srr deployment will continue (badly).\n"
+ e.getClass().getName() + ": " + e.getMessage());
}
}
private void copyConfigFiles(File sarFile, File confDir) throws IOException
{
JarFile jar = new JarFile(sarFile);
Enumeration<JarEntry> en = jar.entries();
while (en.hasMoreElements() == true)
{
JarEntry entry = en.nextElement();
String name = entry.getName();
if (!name.startsWith("conf/"))
{
continue;
}
if (entry.isDirectory())
{
// everything is in one directory
continue;
}
String fname = name.substring("conf/".length());
File f = new File(confDir, fname);
if (f.exists())
{
// we do not override an existing file
continue;
}
// copy the file into the conf directory
InputStream is = jar.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(f);
byte[] buffer = new byte[5000];
int count = 0;
do
{
count = is.read(buffer);
if (count > 0)
{
fos.write(buffer, 0, count);
}
}
while (count > 0);
fos.close();
JipletLogger.info("Copied file " + fname
+ " into the configuration directory");
}
}
}