TransformManagerpublic class TransformManager extends Object implements BaseModule
Fields Summary |
---|
private static TransformManager | transManager | private Document | sourceDocument | private Document | resultDocument | private com.sun.enterprise.util.i18n.StringManager | stringManager | private Logger | logger | private Vector | recoveryList |
Constructors Summary |
---|
public TransformManager()Creates a new instance of TransformManager
|
Methods Summary |
---|
private void | backup(java.lang.String filePath, CommonInfoModel commonInfo)
String backupFilePath = filePath + ".bak";
UpgradeUtils.copyFile(filePath, backupFilePath);
recoveryList.add(filePath);
| public java.lang.String | getName()
return stringManager.getString("upgrade.transform.moduleName");
| public static com.sun.enterprise.tools.upgrade.transform.TransformManager | getTransformManager()
if(transManager == null)
transManager = new TransformManager();
return transManager;
| public static void | main(java.lang.String[] args)
System.setProperty("com.sun.aas.installRoot", "C:\\Softwares\\Sun\\j2eesdk1.4_beta3");
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
//factory.setValidating(true);
factory.setNamespaceAware(true);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document sourceDoc = builder.parse( new File("C:\\temp\\server.xml") );
Document resultDoc = builder.parse( new File("C:\\temp\\domain.xml") );
TransformManager transMan = TransformManager.getTransformManager();
transMan.transform(sourceDoc, resultDoc);
// write out the resultDoc to destination file.
// Use a Transformer for output
TransformerFactory tFactory =
TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(resultDoc);
StreamResult result = new StreamResult(new FileOutputStream("c:\\temp\\domainModified.xml"));
transformer.transform(source, result);
result.getOutputStream().close();
} catch (SAXParseException spe) {
spe.printStackTrace();
} catch (SAXException sxe) {
sxe.printStackTrace();
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (Exception ex){
ex.printStackTrace();
}
| public void | recovery(CommonInfoModel commonInfo)
Enumeration e = recoveryList.elements();
while(e.hasMoreElements()){
String recoverPath = (String)e.nextElement();
String backupPath = recoverPath + ".bak";
try {
UpgradeUtils.copyFile(backupPath, recoverPath);
new File(backupPath).delete();
} catch (IOException ioe) {
logger.log(Level.SEVERE, stringManager.getString("upgrade.realm.recoveryFailureMessage",ioe.getMessage()),new Object[]{recoverPath,ioe});
}
}
| public void | transform(org.w3c.dom.Document source, org.w3c.dom.Document result)
this.sourceDocument = source;
this.resultDocument = result;
try{
Element docEle = sourceDocument.getDocumentElement();
BaseElement baseElement = ElementToObjectMapper.getMapper().getElementObject(docEle.getTagName());
baseElement.transform(docEle, source.getDocumentElement(), result.getDocumentElement());
}catch(Exception ex){
// ****** LOG MESSAGE *************
logger.log(Level.SEVERE, stringManager.getString("upgrade.transform.startFailureMessage",ex.getMessage()),ex);
}
| public boolean | upgrade(CommonInfoModel commonInfo)Method to start upgrade of the transformation module
logger.log(Level.INFO,
stringManager.getString("upgrade.transform.startMessage"));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//factory.setValidating(false);
factory.setNamespaceAware(true);
if(commonInfo.getSourceDomainRootFlag()) {
factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd",Boolean.FALSE);
}
try {
String sourceConfigXMLFile = commonInfo.getSourceConfigXMLFile();
String targetConfigXMLFile = commonInfo.getTargetConfigXMLFile();
String sourceVersion = commonInfo.getSourceVersion();
//Backup the existing domain.xml in target domain directory
backup(targetConfigXMLFile, commonInfo);
//Build the document with target domain.xml
DocumentBuilder builder = factory.newDocumentBuilder();
DocumentBuilder builderDomainXml = factory.newDocumentBuilder();
builderDomainXml.setEntityResolver(
(org.xml.sax.helpers.DefaultHandler)Class.forName(
"com.sun.enterprise.config.serverbeans.ServerValidationHandler").
newInstance());
Document resultDoc = builderDomainXml.parse(new File(targetConfigXMLFile));
//Set commonInfo in BaseElement
BaseElement.setCommonInfoModel(commonInfo);
//Set Entity Resolver to builder if source is not 7X
if(!sourceVersion.equals(UpgradeConstants.VERSION_7X)) {
builder.setEntityResolver(
(org.xml.sax.helpers.DefaultHandler)Class.forName(
"com.sun.enterprise.config.serverbeans.ServerValidationHandler").
newInstance());
}
//Get source doc and transform
Document sourceDoc = builder.parse( new File(sourceConfigXMLFile));
if(sourceDoc.getDocumentElement() != null &&
resultDoc.getDocumentElement() != null)
this.transform(sourceDoc, resultDoc);
else
return false;
//Write out the resultDoc to destination file.
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
//DOCTYPE transformation
if (resultDoc.getDoctype() != null){
String systemValue = resultDoc.getDoctype().getSystemId();
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemValue);
String pubValue = resultDoc.getDoctype().getPublicId();
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pubValue);
}
DOMSource source = new DOMSource(resultDoc);
StreamResult result = new StreamResult(
new FileOutputStream(targetConfigXMLFile));
transformer.transform(source, result);
result.getOutputStream().close();
}catch (Exception ex){
UpdateProgressManager.getProgressManager().setContinueUpgrade(false);
logger.log(Level.SEVERE, stringManager.getString(
"upgrade.transform.startFailureMessage",ex.getMessage()),ex);
logger.log(Level.SEVERE, stringManager.getString(
"upgrade.transform.startFailureCheckAccessMessage"));
return false;
}
//login after transformation in case admin port changed
final File dir = new File (System.getProperty("user.home"));
File store = new File(dir, MemoryHashLoginInfoStore.DEFAULT_STORE_NAME);
try {
MemoryHashLoginInfoStore adminpass = new MemoryHashLoginInfoStore();
String adminPort = DomainsProcessor.getSourceAdminPort(commonInfo.getSourceInstallDir());
final int port = new Integer(adminPort).intValue();
final String user = commonInfo.getAdminUserName();
final String pwd = commonInfo.getAdminPassword();
final LoginInfo login = new LoginInfo("localhost",
port,
user,
pwd );
adminpass.store(login,true);
} catch (Exception e) {
logger.warning(e.getMessage());
}
return true;
|
|