Axis2ServiceDeployer

De Dos Ideas.
Saltar a: navegación, buscar
   package com.dosideas.spring;
   
   import java.io.ByteArrayOutputStream;
   import java.io.File;
   import java.io.FileNotFoundException;
   import java.io.FileOutputStream;
   import java.io.IOException;
   import java.io.InputStream;
   import org.springframework.beans.BeansException;
   import org.springframework.beans.factory.InitializingBean;
   import org.springframework.context.ApplicationContext;
   import org.springframework.context.ApplicationContextAware;
   import org.springframework.web.context.WebApplicationContext;
   
   /**
    *
    * @author ejgarcia
    */
   public class Axis2ServiceDeployer implements InitializingBean, ApplicationContextAware {
       /** Carpeta donde se van a desplegar los servicios. 
        * Debe coincidir la propiedad axis2.repository.path, declarada como
        * init-param en la definición del AxisServlet en el archivo web.xml
        */ 
       protected String servicesDeploymentFolder = null;
       /** Nombre del servicio. El nombre debe coincidir con el wsdl */
       protected String serviceName = null;
       private WebApplicationContext ctx;
       
       public String getServiceName() {
           return serviceName;
       }
       
       public void setServiceName(String serviceName) {
           this.serviceName = serviceName;
       }
       
       public String getServicesDeploymentFolder() {
           return servicesDeploymentFolder;
       }
       
       public void setServicesDeploymentFolder(String servicesDeploymentFolder) {
           this.servicesDeploymentFolder = servicesDeploymentFolder;
       }
       
       public void afterPropertiesSet() {
           /** creo la estructura de directorios default */
           File servicesDeploymentFolderFile = new File(servicesDeploymentFolder);
           servicesDeploymentFolderFile.mkdirs();
           
           File serviceFolderFile = 
               new File(servicesDeploymentFolderFile, "services" + 
                   File.separator + serviceName + File.separator + "META-INF");
           serviceFolderFile.mkdirs();
           
           /** Copio los recursos al directorio */
           String serviceFileResourcePath = "services/" + serviceName + "/META-INF/services.xml";
           String serviceWsdlResourcePath = "services/" + serviceName + "/META-INF/" + serviceName + ".wsdl";
           
           String serviceFileClassPath = "/WEB-INF/" + serviceFileResourcePath;
           String serviceWsdlClassPath = "/WEB-INF/" + serviceWsdlResourcePath;
           //ClassLoader cl = this.getClass().getClassLoader();
           InputStream serviceIn = ctx.getServletContext().getResourceAsStream(serviceFileClassPath);
           InputStream wsdlIn = ctx.getServletContext().getResourceAsStream(serviceWsdlClassPath);
           System.err.println("Recurso 2: " + serviceFileClassPath + " : " + serviceIn);
           System.err.println("Recurso 2: " + serviceWsdlClassPath + " : " + wsdlIn);
           copyInputStreamToFile(serviceIn, new File(servicesDeploymentFolderFile, serviceFileResourcePath));
           copyInputStreamToFile(wsdlIn, new File(servicesDeploymentFolderFile, serviceWsdlResourcePath));
       }
       
       private void copyInputStreamToFile(InputStream in, File file) {
           if (in == null){System.err.println("No se pudo encontrar... "); return;}
           FileOutputStream outToFile = null;
           ByteArrayOutputStream out = null;
           try {
               byte[] buffer = new byte[10000];
               out = new ByteArrayOutputStream();
               outToFile = new FileOutputStream(file);
               //int counter = 0;
               int bytesRead = 0;
               while ((bytesRead = in.read(buffer, 0, buffer.length)) > 0) {
                   System.err.println("bytes leidos: " + bytesRead);
                   out.write(buffer, 0, bytesRead);
                   //counter += bytesRead;
                   //if (bytesRead < buffer.length) break;
               }
               out.writeTo(outToFile);
               
               in.close();
               out.close();
               outToFile.close();
           } catch (FileNotFoundException ex) {
               ex.printStackTrace();
           } catch (IOException ex) {
               ex.printStackTrace();
           } finally {
               try {
                   if (in != null) in.close();
                   if (outToFile != null) outToFile.close();
                   if (out != null) out.close();
               } catch (IOException ex) {
                   ex.printStackTrace();
               }
           }
       }
       
       public void setApplicationContext(ApplicationContext arg0) throws BeansException {
           this.ctx = (WebApplicationContext)arg0;
       }
   }