package org.jboss.management.j2ee;
import org.jboss.logging.Logger;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.net.URL;
import java.net.URLClassLoader;
public abstract class J2EEDeployedObject
extends J2EEManagedObject
implements J2EEDeployedObjectMBean
{
public static final int APPLICATION = 0;
public static final int WEB = 1;
public static final int EJB = 2;
public static final int RAR = 3;
public static final int SAR = 4;
public static final int JBOSS = 5;
public static final int JAWS = 6;
public static final int CMP = 7;
public static final int JBOSS_WEB = 8;
private static final String[] sDescriptors = new String[]{
"META-INF/application.xml",
"WEB-INF/web.xml",
"META-INF/ejb-jar.xml",
"META-INF/ra.xml",
"META-INF/jboss-service.xml",
"META-INF/jboss.xml",
"META-INF/jaws.xml",
"META-INF/jbosscmp-jdbc.xml",
"WEB-INF/jboss-web.xml",
};
private String mDeploymentDescriptor;
public static String getDeploymentDescriptor(URL pJarUrl, int pType)
{
Logger log = Logger.getLogger(J2EEDeployedObject.class);
if (pJarUrl == null)
{
return "";
}
String lDD = null;
Reader lInput = null;
StringWriter lOutput = null;
try
{
if (pJarUrl.toString().endsWith("service.xml"))
{
lInput = new InputStreamReader(pJarUrl.openStream());
}
else
{
log.debug("File: " + pJarUrl + ", descriptor: " + sDescriptors[pType]);
ClassLoader localCl = new URLClassLoader(new URL[]{pJarUrl});
InputStream lStream = localCl.getResourceAsStream(sDescriptors[pType]);
if (lStream == null)
{
return null;
}
lInput = new InputStreamReader(lStream);
}
lOutput = new StringWriter();
char[] lBuffer = new char[1024];
int lLength = 0;
while ((lLength = lInput.read(lBuffer)) > 0)
{
lOutput.write(lBuffer, 0, lLength);
}
lDD = lOutput.toString();
}
catch (Exception e)
{
log.error("failed to get deployment descriptor", e);
}
finally
{
if (lInput != null)
{
try
{
lInput.close();
}
catch (Exception e)
{
}
}
if (lOutput != null)
{
try
{
lOutput.close();
}
catch (Exception e)
{
}
}
}
return lDD;
}
public J2EEDeployedObject(String pType,
String pName,
ObjectName pParent,
String pDeploymentDescriptor)
throws
MalformedObjectNameException,
InvalidParentException
{
super(pType, pName, pParent);
mDeploymentDescriptor = pDeploymentDescriptor;
}
public String getdeploymentDescriptor()
{
return mDeploymentDescriptor;
}
public String getserver()
{
return "unknown server name";
}
public String toString()
{
return "J2EEDeployedObject { " + super.toString() + " } [ " +
"deployment descriptor: " + mDeploymentDescriptor +
" ]";
}
}