001/**
002 * Copyright 2004-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.student.remote.impl.mojo;
017
018import java.io.File;
019import java.io.FileNotFoundException;
020import java.io.FileOutputStream;
021import java.io.PrintStream;
022import org.kuali.student.contract.model.Service;
023
024import org.kuali.student.contract.model.ServiceContractModel;
025import org.kuali.student.contract.model.util.ModelFinder;
026import org.kuali.student.contract.writer.XmlWriter;
027
028/**
029 *
030 * @author nwright
031 */
032public class RemoteImplServiceSpringBeanWriter {
033
034    private ServiceContractModel model;
035    private ModelFinder finder;
036    private String directory;
037    private String rootPackage;
038    private XmlWriter out;
039    private String servKey;
040
041    public RemoteImplServiceSpringBeanWriter(ServiceContractModel model,
042            String directory,
043            String rootPackage,
044            String servKey) {
045        this.model = model;
046        this.finder = new ModelFinder(model);
047        this.directory = directory;
048        this.rootPackage = rootPackage;
049        this.servKey = servKey;
050    }
051
052    private void indentPrintln(String line) {
053        out.indentPrintln(line);
054    }
055
056    /**
057     * Write out the entire file
058     *
059     * @param out
060     */
061    public void write() {
062        initXmlWriter();
063        Service service = finder.findService(servKey);
064        writeService(service);
065    }
066
067    private void writeService(Service service) {
068
069        indentPrintln("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
070        indentPrintln("<!--");
071        indentPrintln(" Copyright 2008-2014 The Kuali Foundation");
072        indentPrintln(" ");
073        indentPrintln(" Licensed under the Educational Community License, Version 2.0 (the \"License\");");
074        indentPrintln(" you may not use this file except in compliance with the License.");
075        indentPrintln(" You may obtain a copy of the License at");
076        indentPrintln(" ");
077        indentPrintln(" http://www.opensource.org/licenses/ecl2.php");
078        indentPrintln(" ");
079        indentPrintln(" Unless required by applicable law or agreed to in writing, software");
080        indentPrintln(" distributed under the License is distributed on an \"AS IS\" BASIS,");
081        indentPrintln(" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
082        indentPrintln(" See the License for the specific language governing permissions and");
083        indentPrintln(" limitations under the License.");
084        indentPrintln("-->");
085        indentPrintln("<beans xmlns=\"http://www.springframework.org/schema/beans\"");
086        indentPrintln("       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
087        indentPrintln("       xmlns:util=\"http://www.springframework.org/schema/util\"");
088        indentPrintln("       xmlns:p=\"http://www.springframework.org/schema/p\"");
089        indentPrintln("       xmlns:aop=\"http://www.springframework.org/schema/aop\"");
090        indentPrintln("       xmlns:tx=\"http://www.springframework.org/schema/tx\"");
091        indentPrintln("       xsi:schemaLocation=\"http://www.springframework.org/schema/beans");
092        indentPrintln("                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd");
093        indentPrintln("                           http://www.springframework.org/schema/tx");
094        indentPrintln("                           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd");
095        indentPrintln("                           http://www.springframework.org/schema/aop");
096        indentPrintln("                           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd");
097        indentPrintln("                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd\">");
098        indentPrintln("");
099        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}