Posted on 2011-01-02 17:36
在路上... 閱讀(12339)
評論(0) 編輯 收藏 所屬分類:
JAVA相關
通常,如果需要在應用中使用tomcat的jndi數據源,需要修改context配置,例如
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/app" docBase="E:\appweb">
<Resource name="jndi/ds" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@192.168.1.3:1522:orcl" username="111" password="222" maxActive="20" maxIdle="10" maxWait="-1" />
</Context>
但是使用intellij idea開發時,tomcat插件會自動維護該文件,為此我們對tomcat插件可以做些適當的修改。
1、設計目標
目標:在deploy部署時,自動檢查web程序根目錄,也就是docBase/META-INF/jndi-resource.xml文件是否存在,如果有,則該文件定義的recource資源自動加入context節點中部署
這樣程序就可以很方便的使用tomcat jndi數據源了,如果切換當生產環境,例如weblogic,也不用修改什么。
2、實現
修改C:\Program Files\JetBrains\IntelliJ IDEA 9.0.1\lib\src\src_tomcat.zip中的org.jetbrains.idea.tomcat.TomcatDeploymentProviderw文件
a)增加一個方法
1
private static void addAppResource(Element ctx,String deploymentPath)
{
2
File f = new File(deploymentPath+File.separator+"META-INF"+File.separator+"jndi-resource.xml");
3
if (f.exists())
{
4
try
{
5
if(ctx.getContentSize()>0) return;
6
Document doc = TomcatUtil.loadXMLFile(f.getPath());//builder.build(new FileInputStream(f));
7
List nodes=doc.getRootElement().getChildren("Resource");
8
for(int i=0;i<nodes.size();i++)
{
9
Element n=(Element)nodes.get(i);
10
ctx.addContent((Element)n.clone());
11
System.out.println("Load JNDI resource from "+f.getPath()+".");
12
}
13
} catch (Exception ex)
{
14
LOG.info("加載應用的資源配置錯誤:"+ex.getMessage());
15
}
16
}
17
}
b)修改調用
1
private static void addApplicationContext(TomcatModuleDeploymentModel tomcatModuleDeploymentModel) throws ExecutionException
{
2
try
{
3
TomcatModel serverModel = (TomcatModel)tomcatModuleDeploymentModel.getServerModel();
4
String contextPath = getContextPath(tomcatModuleDeploymentModel);
5
6
Element contextElement = TomcatUtil.findContextElement(serverModel.getSourceBaseDirectoryPath(), contextPath, tomcatModuleDeploymentModel);
7
8
if (contextElement == null)
{
9
contextElement = new Element(CONTEXT_ELEMENT_NAME);
10
//contextElement.addContent((Comment)TomcatConstants.CONTEXT_COMMENT.clone());
11
}
12
13
final String deploymentPath = TomcatUtil.getDeploymentPath(tomcatModuleDeploymentModel);
14
if (deploymentPath == null)
{
15
throw new ExecutionException(TomcatBundle.message("exception.text.neither.exploded.directory.nor.jar.file.configured"));
16
}
17
18
if (!new File(deploymentPath).exists())
{
19
throw new ExecutionException(TomcatBundle.message("exception.text.file.not.found.for.web.module", deploymentPath));
20
}
21
22
//remove unpacked WAR directory
23
if(DeploymentSource.FROM_JAR == tomcatModuleDeploymentModel.getDeploymentSource())
{
24
final String contextXML = TomcatUtil.getContextXML(serverModel.getSourceBaseDirectoryPath(), contextPath);
25
final String xmlName = new File(contextXML).getName();
26
final String dirName = xmlName.substring(0, xmlName.length() - 4);
27
28
final Document serverXmlDocument = TomcatUtil.loadXMLFile(TomcatUtil.serverXML(serverModel.getBaseDirectoryPath()));
29
final Element localHost = TomcatUtil.findLocalHost(serverXmlDocument.getRootElement());
30
31
final String appBase = localHost.getAttributeValue(APP_BASE_ATTR);
32
FileUtil.delete(new File(appBase, dirName));
33
}
34
35
contextElement.setAttribute(PATH_ATTR, contextPath);
36
contextElement.setAttribute(DOC_BASE_ATTR, deploymentPath);
37
38
if(serverModel.versionHigher(TomcatPersistentData.VERSION50))
{
39
final String contextXML = TomcatUtil.getContextXML(serverModel.getBaseDirectoryPath(), contextPath);
40
final File targetContextXmlFile = new File(contextXML);
41
targetContextXmlFile.getParentFile().mkdirs();
42
43
final Document xmlDocument;
44
if(contextElement.getDocument() != null && contextElement.isRootElement())
{
45
xmlDocument = (Document)contextElement.getDocument().clone();
46
}
47
else
{
48
xmlDocument = new Document();
49
xmlDocument.setRootElement((Element)contextElement.clone());
50
}
51
//新增調用方法,處理額外的resource
52
addAppResource(xmlDocument.getRootElement(),deploymentPath);
53
TomcatUtil.saveXMLFile(xmlDocument, targetContextXmlFile.getPath(), true);
54
}
55
else
{
56
String root = FileUtil.toSystemDependentName(TomcatUtil.getGeneratedFilesPath(serverModel));
57
String scratchdir = root + File.separator + TomcatConstants.CATALINA_WORK_DIRECTORY_NAME + File.separator
58
+ new File(TomcatUtil.getContextXML(serverModel.getBaseDirectoryPath(), contextPath)).getName();
59
new File(scratchdir).mkdirs();
60
61
contextElement.setAttribute(WORKDIR_ATTR, scratchdir);
62
63
addOrRemoveContextElementInServerXml(serverModel, contextPath, contextElement);
64
}
65
}
66
catch (RuntimeConfigurationException e)
{
67
throw new ExecutionException(e.getMessage());
68
}
69
}
編譯源代碼,使用如下命令行
javac -classpath "tomcat.jar;C:\Program Files\JetBrains\IntelliJ IDEA 9.0.1\lib\idea.jar;C:\Program Files\JetBrains\IntelliJ IDEA 9.0.1\lib\openapi.jar;C:\Program Files\JetBrains\IntelliJ IDEA 9.0.1\lib\jdom.jar;C:\Program Files\JetBrains\IntelliJ IDEA 9.0.1\plugins\JavaEE\lib\javaee-openapi.jar;C:\Program Files\JetBrains\IntelliJ IDEA 9.0.1\lib\util.jar;C:\Program Files\JetBrains\IntelliJ IDEA 9.0.1\lib\annotations.jar;C:\Program Files\JetBrains\IntelliJ IDEA 9.0.1\plugins\DatabaseSupport\lib\database-openapi.jar" -target 1.5 -source 1.5 *.java
編譯完成之后,將新的class覆蓋到tomcat.jar中就可以了。
3、測試
web應用的根目錄加入META-INF/jndi-resource.xml,內容如下:
<r>
<Resource name="jndi/ds" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@192.168.1.3:1522:orcl" username="111" password="222" maxActive="20" maxIdle="10" maxWait="-1" />
</r>
經過使用idea 9.0+tomcat 6測試通過,附件:
tomcat插件,改為.jar