View Javadoc
1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.student.remote.impl.mojo;
17  
18  import java.io.File;
19  import java.io.FileNotFoundException;
20  import java.io.FileOutputStream;
21  import java.io.PrintStream;
22  import org.kuali.student.contract.model.Service;
23  
24  import org.kuali.student.contract.model.ServiceContractModel;
25  import org.kuali.student.contract.model.util.ModelFinder;
26  import org.kuali.student.contract.writer.XmlWriter;
27  
28  /**
29   *
30   * @author nwright
31   */
32  public class RemoteImplServiceSpringBeanWriter {
33  
34      private ServiceContractModel model;
35      private ModelFinder finder;
36      private String directory;
37      private String rootPackage;
38      private XmlWriter out;
39      private String servKey;
40  
41      public RemoteImplServiceSpringBeanWriter(ServiceContractModel model,
42              String directory,
43              String rootPackage,
44              String servKey) {
45          this.model = model;
46          this.finder = new ModelFinder(model);
47          this.directory = directory;
48          this.rootPackage = rootPackage;
49          this.servKey = servKey;
50      }
51  
52      private void indentPrintln(String line) {
53          out.indentPrintln(line);
54      }
55  
56      /**
57       * Write out the entire file
58       *
59       * @param out
60       */
61      public void write() {
62          initXmlWriter();
63          Service service = finder.findService(servKey);
64          writeService(service);
65      }
66  
67      private void writeService(Service service) {
68  
69          indentPrintln("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
70          indentPrintln("<!--");
71          indentPrintln(" Copyright 2008-2014 The Kuali Foundation");
72          indentPrintln(" ");
73          indentPrintln(" Licensed under the Educational Community License, Version 2.0 (the \"License\");");
74          indentPrintln(" you may not use this file except in compliance with the License.");
75          indentPrintln(" You may obtain a copy of the License at");
76          indentPrintln(" ");
77          indentPrintln(" http://www.opensource.org/licenses/ecl2.php");
78          indentPrintln(" ");
79          indentPrintln(" Unless required by applicable law or agreed to in writing, software");
80          indentPrintln(" distributed under the License is distributed on an \"AS IS\" BASIS,");
81          indentPrintln(" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
82          indentPrintln(" See the License for the specific language governing permissions and");
83          indentPrintln(" limitations under the License.");
84          indentPrintln("-->");
85          indentPrintln("<beans xmlns=\"http://www.springframework.org/schema/beans\"");
86          indentPrintln("       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
87          indentPrintln("       xmlns:util=\"http://www.springframework.org/schema/util\"");
88          indentPrintln("       xmlns:p=\"http://www.springframework.org/schema/p\"");
89          indentPrintln("       xmlns:aop=\"http://www.springframework.org/schema/aop\"");
90          indentPrintln("       xmlns:tx=\"http://www.springframework.org/schema/tx\"");
91          indentPrintln("       xsi:schemaLocation=\"http://www.springframework.org/schema/beans");
92          indentPrintln("                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd");
93          indentPrintln("                           http://www.springframework.org/schema/tx");
94          indentPrintln("                           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd");
95          indentPrintln("                           http://www.springframework.org/schema/aop");
96          indentPrintln("                           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd");
97          indentPrintln("                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd\">");
98          indentPrintln("");
99          indentPrintln("");
100         indentPrintln("    <bean id=\"" + service.getKey().toLowerCase() + "ServiceRemoteImpl\" class=\"" 
101                 + RemoteImplServiceWriter.calcPackage(service.getKey(), rootPackage) + "." + RemoteImplServiceWriter.calcClassName(service.getKey()) + "\">");
102         indentPrintln("    <!-- ********************************************************************************************");
103         indentPrintln("    The host url or other properties may be configured here BUT the host url should really be set via configuration ");
104         indentPrintln("    so that you can have your remote service point to local host during development, your TEST servers during testing, ");
105         indentPrintln("    QA for your QA deployment and your PROD servers for production by just changing the config file on that server, for example:");
106         indentPrintln("    ");
107         indentPrintln("    Add something like this to your config (in userhome/kuali/dev/ks-xxx-config.xml on the machine that is running the application's");
108         indentPrintln("    war file:");
109         indentPrintln("    ");
110         indentPrintln("    Notes: ");
111         indentPrintln("      The first parameter provides a default server for all remote services");
112         indentPrintln("      The second parameter allows you to override the server on a service by service basis, in this case the " + service.getKey().toLowerCase() + " service");
113         indentPrintln("      ");
114         indentPrintln("      <config>");
115         indentPrintln("        ...  ");
116         indentPrintln("            <param name=\"remote.service.host.url\">http://qaserver.myschool.edu/kscm</param>");
117         indentPrintln("            <param name=\"remote.service.host.url." + service.getKey().toLowerCase() + "\">http://my" + service.getKey().toLowerCase() + "serviceserver.myschool.edu/kscm</param>");
118         indentPrintln("        ...");
119         indentPrintln("      </config>");
120         indentPrintln("    ");
121         indentPrintln("          You can so something like this only if you want to hardwire in as part of the war the host url to a particular server");
122         indentPrintln("          perhaps as part of configuring a unit or functional test:");
123         indentPrintln("               <property name=\"hostUrl\" value=\"http://localhost/kscm\" />");
124         indentPrintln("    **************************************************************************************************** -->");
125         indentPrintln("    </bean>");
126         indentPrintln("");
127         indentPrintln("    <bean id=\"ks.exp." + service.getKey().toLowerCase() + "Service\" class=\"org.kuali.rice.ksb.api.bus.support.ServiceBusExporter\">");
128         indentPrintln("        <property name=\"serviceDefinition\">");
129         indentPrintln("            <bean class=\"org.kuali.rice.ksb.api.bus.support.SoapServiceDefinition\">");
130         indentPrintln("                <property name=\"jaxWsService\" value=\"true\" />");
131         indentPrintln("                <property name=\"service\" ref=\"" + service.getKey().toLowerCase() + "ServiceRemoteImpl\" />");
132         indentPrintln("                <property name=\"serviceInterface\" value=\"" + service.getImplProject() + "." + service.getName() + "\" />");
133         indentPrintln("                <property name=\"localServiceName\" value=\"" + service.getName() + "\" />");
134         indentPrintln("                <property name=\"serviceNameSpaceURI\" value=\"http://student.kuali.org/wsdl/" + service.getKey().toLowerCase() + "\" />");
135         indentPrintln("                <property name=\"busSecurity\" value=\"false\" />");
136         indentPrintln("            </bean>");
137         indentPrintln("        </property>");
138         indentPrintln("    </bean>");
139         indentPrintln("");
140         indentPrintln("</beans>");
141 
142     }
143 
144     private void initXmlWriter() {
145         String fileName = "ksb-" + servKey.toLowerCase() + "-service-remote-impl.xml";
146 
147         File dir = new File(this.directory + "/main/resources/ksb");
148 
149         if (!dir.exists()) {
150             if (!dir.mkdirs()) {
151                 throw new IllegalStateException("Could not create directory "
152                         + this.directory + "/main/resources");
153             }
154         }
155 
156         String dirStr = this.directory + "/main/resources/ksb";
157         File dirFile = new File(dirStr);
158         if (!dirFile.exists()) {
159             if (!dirFile.mkdirs()) {
160                 throw new IllegalStateException(
161                         "Could not create directory " + dirStr);
162             }
163         }
164         try {
165             PrintStream out = new PrintStream(new FileOutputStream(
166                     dirStr + "/"
167                     + fileName, false));
168             this.out = new XmlWriter(out, 0);
169         } catch (FileNotFoundException ex) {
170             throw new IllegalStateException(ex);
171         }
172     }
173 }