FileDocCategorySizeDatePackage
Domain2ServerTransformer.javaAPI DocGlassfish v2 API8775Fri May 04 22:33:56 BST 2007com.sun.enterprise.admin.server.core.mbean.config

Domain2ServerTransformer

public final class Domain2ServerTransformer extends Object
Class to convert domain.xml into server.xml. Currently has the final implementation and has a single method called "transform".

Fields Summary
private static final String
ORIG_EXT
private static final String
XSL_NAME
private static final String
DOCTYPE_TOKEN
private static final String
SERVER_DTD_NAME
private static final String
SERVER_DTD_PATH
private static String
XSL_PATH
private TransformerFactory
tFactory
private final String
domainXmlPath
private final String
serverXmlPath
private final String
origServerXmlPath
Constructors Summary
public Domain2ServerTransformer(String domainXmlPath, String serverXmlPath)


       
                                      
        this.domainXmlPath = domainXmlPath;
        this.serverXmlPath = serverXmlPath;
        origServerXmlPath  = serverXmlPath + ORIG_EXT;
        createFactory();
    
Methods Summary
private voidconvert()

        try {
            /* xsl */
            final InputStream  xslStream    = getXslStream();
            final StreamSource xsl          = new StreamSource(xslStream);

            /* domain xml */
            final File domainXmlFile        = new File(domainXmlPath);
            System.out.println("The source xml = " + domainXmlFile.getAbsolutePath());
            final StreamSource xml          = new StreamSource(domainXmlFile);
            
            /* server xml */
            final File serverXmlFile        = new File(serverXmlPath);
            final StreamResult out          = new StreamResult(serverXmlFile); 

            final Transformer transformer   = tFactory.newTransformer(xsl);
            
            transformer.transform(xml, out);
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    
private voidcreateFactory()

        try {
            tFactory = TransformerFactory.newInstance();
            System.out.println("Created xform factory = " + tFactory);
        }
        catch(Exception e) {
            System.out.println("Exception while creating transformer factory");
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    
private java.lang.StringgetDtdPathForServerXml()

        /* unfortunately, this has to be formed 
         * by the standard technique of the string concat!
        */
		String PREFIX = "file://";
        if (OS.isWindows()) {
            PREFIX = PREFIX+"/";
        }
        return ( PREFIX + SERVER_DTD_PATH );
        /*
        BufferedInputStream bis = null;
        String dtd = null;
		try {
			final String PREFIX = "file:///";
            final String DTD_EXT = ".dtd";
            final File f = new File(serverXmlPath);
            bis = new BufferedInputStream 
                    (new FileInputStream(f));
            byte[] bytes = new byte[1024];
            int bytesRead = 0;
			while((bytesRead = bis.read(bytes)) != -1) {
                final String line = new String(bytes);
				final int start = line.indexOf(PREFIX);
				if (start != -1) {
					final int end = line.lastIndexOf(DTD_EXT);
					dtd = line.substring(start, end);
                    dtd = dtd + DTD_EXT;
					break;
				}
			}
        }
        catch(Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            try {
                if (bis != null)
                    bis.close();
            }
            catch(Exception e) {}
        }
        return ( dtd );
        */
    
private java.io.InputStreamgetXslStream()

        return ( new FileInputStream(XSL_PATH) );
        /*
        final Class thisClass = Domain2ServerTransformer.class;
        final String packageName = 
                thisClass.getPackage().getName();
        final String modifiedName = packageName.replace('.', '/');
        final String xslStreamName = modifiedName + "/" + XSL_NAME;
        System.out.println("The xslStreamName = " + xslStreamName);
        return (thisClass.getClassLoader().getResourceAsStream(xslStreamName));
        */
    
private voidreplaceDocTypePath(java.lang.String dtdPath)

        try {

            final File tmpFile          = File.createTempFile("temp", ".xml");
            final String tmpFilePath    = tmpFile.getAbsolutePath();
            FileUtils.copy(serverXmlPath, tmpFilePath);

            final TokenValue tv = new TokenValue(DOCTYPE_TOKEN, dtdPath);
            System.out.println("TV = " + tv);
            final TokenValueSet ts = new TokenValueSet();
            ts.add(tv);
            final LineTokenReplacer replacer = new LineTokenReplacer(ts);
            replacer.replace(tmpFilePath, serverXmlPath);
            tmpFile.delete();
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    
public final voidtransform()

        this.transform(true);
    
public final voidtransform(boolean backup)

        if (backup) {
            try {
                if (new File(serverXmlPath).exists()) {
                    FileUtils.copy(serverXmlPath, origServerXmlPath);
                }
                System.out.println("ServerXmlPath = " + serverXmlPath);
                System.out.println("DomainXmlPath = " + domainXmlPath);
                System.out.println("ServerDtdPath = " + SERVER_DTD_PATH);
                System.out.println("XslPath = " + XSL_PATH);
            }
            catch (Exception e) {
                System.out.println("Could not backup server.xml before xform");
                throw new RuntimeException(e);
            }
        }
        final String dtdPath = getDtdPathForServerXml();
        convert();
        replaceDocTypePath(dtdPath);