001    /**
002     * Copyright 2004-2013 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     */
016    package org.kuali.student.remote.impl.mojo;
017    
018    import java.io.File;
019    import java.io.FileNotFoundException;
020    import java.io.FileOutputStream;
021    import java.io.PrintStream;
022    import org.kuali.student.contract.model.Service;
023    
024    import org.kuali.student.contract.model.ServiceContractModel;
025    import org.kuali.student.contract.model.util.ModelFinder;
026    import org.kuali.student.contract.writer.XmlWriter;
027    
028    /**
029     *
030     * @author nwright
031     */
032    public class RemoteImplServiceSpringBeanWriter {
033        
034        private ServiceContractModel model;
035        private ModelFinder finder;
036        private String directory;
037        private String rootPackage;
038        private XmlWriter out;
039        
040        public RemoteImplServiceSpringBeanWriter(ServiceContractModel model,
041                String directory,
042                String rootPackage) {
043            this.model = model;
044            this.finder = new ModelFinder(model);
045            this.directory = directory;
046            this.rootPackage = rootPackage;
047        }
048    
049        /**
050         * Write out the entire file
051         *
052         * @param out
053         */
054        public void write() {
055            initXmlWriter();
056            for (Service service : model.getServices()) {
057                writeService(service);
058            }
059        }
060        
061        private void writeService(Service service) {
062            out.println("");
063            
064            out.indentPrintln("<bean id=\"" + service.getKey().toLowerCase() + "Service\" class=\"" + RemoteImplServiceWriter.calcPackage(service.getKey(), rootPackage) + "." + RemoteImplServiceWriter.calcClassName(service.getKey()) + "\">");
065            out.incrementIndent();
066            out.indentPrintln("<property name=\"hostUrl\" value=\"http://env2.ks.kuali.org\" />");
067            out.decrementIndent();
068            out.indentPrintln("</bean>");
069            out.println("");
070            
071            out.indentPrintln("<bean id=\"ks.exp." + service.getKey().toLowerCase() + "Service\" class=\"org.kuali.rice.ksb.api.bus.support.ServiceBusExporter\">");
072            out.incrementIndent();
073            out.indentPrintln("<property name=\"serviceDefinition\">");
074            out.incrementIndent();
075            out.indentPrintln("<bean class=\"org.kuali.rice.ksb.api.bus.support.SoapServiceDefinition\">");
076            out.incrementIndent();
077            out.indentPrintln("<property name=\"jaxWsService\" value=\"true\" />");
078            out.indentPrintln("<property name=\"service\" ref=\"" + service.getKey().toLowerCase() + "Service\" />");
079            out.indentPrintln("<property name=\"serviceInterface\" value=\"" + service.getImplProject() + "." + service.getName() + "\" />");
080            out.indentPrintln("<property name=\"localServiceName\" value=\"" + service.getName() + "\" />");
081            out.indentPrintln("<property name=\"serviceNameSpaceURI\" value=\"http://student.kuali.org/wsdl/" + service.getKey().toLowerCase () + "\" />");
082            out.indentPrintln("<property name=\"busSecurity\" value=\"false\" />");
083            out.decrementIndent();
084            out.indentPrintln("</bean>");
085            out.decrementIndent();
086            out.indentPrintln("</property>");
087            out.decrementIndent();
088            out.indentPrintln("</bean>");
089        }
090        
091        private void initXmlWriter() {
092            String fileName = "ksb-remote-impl-generated.xml";
093            
094            File dir = new File(this.directory + "/main/resources");
095            
096            if (!dir.exists()) {
097                if (!dir.mkdirs()) {
098                    throw new IllegalStateException("Could not create directory "
099                            + 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    }