View Javadoc

1   /*
2    * Copyright 2009 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.osedu.org/licenses/ECL-2.0
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      
40      public RemoteImplServiceSpringBeanWriter(ServiceContractModel model,
41              String directory,
42              String rootPackage) {
43          this.model = model;
44          this.finder = new ModelFinder(model);
45          this.directory = directory;
46          this.rootPackage = rootPackage;
47      }
48  
49      /**
50       * Write out the entire file
51       *
52       * @param out
53       */
54      public void write() {
55          initXmlWriter();
56          for (Service service : model.getServices()) {
57              writeService(service);
58          }
59      }
60      
61      private void writeService(Service service) {
62          out.println("");
63          
64          out.indentPrintln("<bean id=\"" + service.getKey().toLowerCase() + "Service\" class=\"" + RemoteImplServiceWriter.calcPackage(service.getKey(), rootPackage) + "." + RemoteImplServiceWriter.calcClassName(service.getKey()) + "\">");
65          out.incrementIndent();
66          out.indentPrintln("<property name=\"hostUrl\" value=\"http://env2.ks.kuali.org\" />");
67          out.decrementIndent();
68          out.indentPrintln("</bean>");
69          out.println("");
70          
71          out.indentPrintln("<bean id=\"ks.exp." + service.getKey().toLowerCase() + "Service\" class=\"org.kuali.rice.ksb.api.bus.support.ServiceBusExporter\">");
72          out.incrementIndent();
73          out.indentPrintln("<property name=\"serviceDefinition\">");
74          out.incrementIndent();
75          out.indentPrintln("<bean class=\"org.kuali.rice.ksb.api.bus.support.SoapServiceDefinition\">");
76          out.incrementIndent();
77          out.indentPrintln("<property name=\"jaxWsService\" value=\"true\" />");
78          out.indentPrintln("<property name=\"service\" ref=\"" + service.getKey().toLowerCase() + "Service\" />");
79          out.indentPrintln("<property name=\"serviceInterface\" value=\"" + service.getImplProject() + "." + service.getName() + "\" />");
80          out.indentPrintln("<property name=\"localServiceName\" value=\"" + service.getName() + "\" />");
81          out.indentPrintln("<property name=\"serviceNameSpaceURI\" value=\"http://student.kuali.org/wsdl/" + service.getKey().toLowerCase () + "\" />");
82          out.indentPrintln("<property name=\"busSecurity\" value=\"false\" />");
83          out.decrementIndent();
84          out.indentPrintln("</bean>");
85          out.decrementIndent();
86          out.indentPrintln("</property>");
87          out.decrementIndent();
88          out.indentPrintln("</bean>");
89      }
90      
91      private void initXmlWriter() {
92          String fileName = "ksb-remote-impl-generated.xml";
93          
94          File dir = new File(this.directory + "/main/resources");
95          
96          if (!dir.exists()) {
97              if (!dir.mkdirs()) {
98                  throw new IllegalStateException("Could not create directory "
99                          + this.directory + "/main/resources");
100             }
101         }
102         
103         String dirStr = this.directory  + "/main/resources";
104         File dirFile = new File(dirStr);
105         if (!dirFile.exists()) {
106             if (!dirFile.mkdirs()) {
107                 throw new IllegalStateException(
108                         "Could not create directory " + dirStr);
109             }
110         }
111         try {
112             PrintStream out = new PrintStream(new FileOutputStream(
113                     dirStr + "/"
114                     + fileName, false));
115             this.out = new XmlWriter(out, 0);
116         } catch (FileNotFoundException ex) {
117             throw new IllegalStateException(ex);
118         }
119     }
120 }