intellij idea使用tomcat开发时自动部署jndi数据源

通常,如果需要在应用中使用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 >
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值