posts - 33, comments - 46, trackbacks - 0, articles - 2
          通常,如果需要在應(yīng)用中使用tomcat的jndi數(shù)據(jù)源,需要修改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開發(fā)時(shí),tomcat插件會(huì)自動(dòng)維護(hù)該文件,為此我們對(duì)tomcat插件可以做些適當(dāng)?shù)男薷摹?br /> 1、設(shè)計(jì)目標(biāo)
          目標(biāo):在deploy部署時(shí),自動(dòng)檢查web程序根目錄,也就是docBase/META-INF/jndi-resource.xml文件是否存在,如果有,則該文件定義的recource資源自動(dòng)加入context節(jié)點(diǎn)中部署
          這樣程序就可以很方便的使用tomcat jndi數(shù)據(jù)源了,如果切換當(dāng)生產(chǎn)環(huán)境,例如weblogic,也不用修改什么。
          2、實(shí)現(xiàn)
          修改C:\Program Files\JetBrains\IntelliJ IDEA 9.0.1\lib\src\src_tomcat.zip中的org.jetbrains.idea.tomcat.TomcatDeploymentProviderw文件
          a)增加一個(gè)方法
           1private 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()>0return;
           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("加載應(yīng)用的資源配置錯(cuò)誤:"+ex.getMessage());
          15            }

          16        }

          17    }
          b)修改調(diào)用
           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        //新增調(diào)用方法,處理額外的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應(yīng)用的根目錄加入META-INF/jndi-resource.xml,內(nèi)容如下:
          <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>

          經(jīng)過使用idea 9.0+tomcat 6測試通過,附件:tomcat插件,改為.jar
          主站蜘蛛池模板: 邢台市| 新化县| 绩溪县| 万盛区| 大安市| 西丰县| 大方县| 建阳市| 兴和县| 镇远县| 正镶白旗| 民和| 宁德市| 南涧| 保靖县| 思茅市| 闽侯县| 临安市| 潢川县| 汉寿县| 徐州市| 泗洪县| 高淳县| 永清县| 白水县| 亚东县| 尤溪县| 历史| 平顶山市| 湘阴县| 福州市| 灵台县| 邵武市| 楚雄市| 康保县| 灵石县| 郓城县| 奎屯市| 灌南县| 镇平县| 蒙自县|