package org.jboss.varia.deployment.convertor;
import java.io.File;
import java.util.Properties;
import java.util.jar.JarFile;
import java.net.URL;
import javax.management.JMException;
import javax.management.ObjectName;
import org.jboss.deployment.DeploymentInfo;
import org.jboss.system.ServiceMBeanSupport;
public class WebLogicConvertor
extends ServiceMBeanSupport
implements Convertor, WebLogicConvertorMBean
{
private String deployerName;
private String wlVersion;
private String removeTable;
private String datasource;
private String datasourceMapping;
private Properties xslParams;
public String getDeployer()
{
return deployerName;
}
public void setDeployer( String name )
{
if( deployerName != null && name!= null && deployerName != name )
{
try
{
server.invoke(
new ObjectName( deployerName ),
"removeConvertor",
new Object[] { this },
new String[] { this.getClass().getName() }
);
}
catch( JMException jme ) { }
}
if( name != null ) deployerName = name;
}
public String getWlVersion()
{
return wlVersion;
}
public void setWlVersion( String wlVersion )
{
this.wlVersion = wlVersion;
}
public String getRemoveTable()
{
return removeTable;
}
public void setRemoveTable( String removeTable )
{
this.removeTable = removeTable;
}
public String getDatasource()
{
return datasource;
}
public void setDatasource( String datasource )
{
this.datasource = datasource;
}
public String getDatasourceMapping()
{
return datasourceMapping;
}
public void setDatasourceMapping( String datasourceMapping )
{
this.datasourceMapping = datasourceMapping;
}
public void startService()
{
try
{
initXslParams();
server.invoke(
new ObjectName(deployerName),
"addConvertor",
new Object[] { this },
new String[] { Convertor.class.getName() }
);
}
catch( JMException jme )
{
log.error( "Caught exception during startService()", jme );
}
}
public void stopService()
{
if(deployerName != null)
{
try {
server.invoke(
new ObjectName(deployerName),
"removeConvertor",
new Object[] { this },
new String[] { this.getClass().getName() }
);
}
catch( JMException jme )
{
}
}
}
public boolean accepts(URL url)
{
String stringUrl = url.toString();
JarFile jarFile = null;
boolean accepted = false;
try
{
jarFile = new JarFile(url.getPath());
accepted = (jarFile.getEntry("META-INF/weblogic-ejb-jar.xml" ) != null)
&& (stringUrl.endsWith(".wlar") || (stringUrl.endsWith(".wl")))
|| stringUrl.endsWith(".war.wl")
|| stringUrl.endsWith(".ear.wl") ;
jarFile.close();
}
catch(Exception e)
{
log.debug("Couldn't create JarFile for " + url.getPath(), e);
return false;
}
return accepted;
}
public void convert(DeploymentInfo di, File path)
throws Exception
{
Properties xslParams = getXslParams();
JarTransformer.transform(path, xslParams);
}
public Properties getXslParams()
{
if(xslParams == null)
{
log.warn("xmlParams should have been initialized!");
xslParams = initXslParams();
}
xslParams.setProperty("resources_path", "resources/" + wlVersion + "/");
xslParams.setProperty("remove-table", removeTable);
xslParams.setProperty("datasource", datasource);
xslParams.setProperty("datasource-mapping", datasourceMapping);
return xslParams;
}
private Properties initXslParams()
{
xslParams = new Properties();
ClassLoader cl = Thread.currentThread().getContextClassLoader();
URL url = cl.getResource( "standardjboss.xml" );
if( url != null )
xslParams.setProperty( "standardjboss",
new File( url.getFile()).getAbsolutePath() );
else log.debug( "standardjboss.xml not found." );
url = cl.getResource( "standardjbosscmp-jdbc.xml" );
if( url != null )
xslParams.setProperty( "standardjbosscmp-jdbc",
new File( url.getFile()).getAbsolutePath() );
else log.debug( "standardjbosscmp-jdbc.xml not found." );
log.debug( "initialized xsl parameters: " + xslParams );
return xslParams;
}
}