通常,如果需要在应用中使用tomcat的jndi数据源,需要修改context配置,例如




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)修改调用



2

3



4



5

6

7

8



9

10

11

12

13



14

15

16

17

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
}



2



3

4

5

6

7

8



9

10

11

12

13

14



15

16

17

18



19

20

21

22

23



24

25

26

27

28

29

30

31

32

33

34

35

36

37

38



39

40

41

42

43

44



45

46

47



48

49

50

51

52

53

54

55



56

57

58

59

60

61

62

63

64

65

66



67

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,内容如下:


