1   /*
2    * Copyright 2003 - 2013 The eFaps Team
3    *
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-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   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  package org.efaps.rest;
22  
23  import java.io.File;
24  import java.io.FilenameFilter;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import javax.ws.rs.GET;
29  import javax.ws.rs.Path;
30  import javax.ws.rs.QueryParam;
31  import javax.ws.rs.core.Response;
32  
33  import org.efaps.admin.EFapsSystemConfiguration;
34  import org.efaps.admin.KernelSettings;
35  import org.efaps.admin.common.SystemConfiguration;
36  import org.efaps.update.schema.program.esjp.ESJPCompiler;
37  import org.efaps.update.schema.program.jasperreport.JasperReportCompiler;
38  import org.efaps.update.schema.program.staticsource.CSSCompiler;
39  import org.efaps.update.schema.program.staticsource.JavaScriptCompiler;
40  import org.efaps.update.schema.program.staticsource.WikiCompiler;
41  import org.efaps.update.util.InstallationException;
42  import org.efaps.util.EFapsException;
43  
44  /**
45   * Rest API to compile the different program froms eFaps.
46   *
47   * @author The eFaps Team
48   * @version $Id$
49   */
50  @Path("/compile")
51  public class Compile
52      extends AbstractRest
53  {
54  
55      /**
56       * Called to compile java, css etc.
57       *
58       * @param _type type tobe compiled
59       * @return Response
60       */
61      @GET
62      public Response compile(@QueryParam("type") final String _type)
63      {
64          boolean success = false;
65          try {
66              if (hasAccess()) {
67                  AbstractRest.LOG.info("===Starting Compiler via REST===");
68                  if ("java".equalsIgnoreCase(_type)) {
69                      AbstractRest.LOG.info("==Compiling Java==");
70                      new ESJPCompiler(getClassPathElements()).compile(null, false);
71                  } else if ("css".equalsIgnoreCase(_type)) {
72                      AbstractRest.LOG.info("==Compiling CSS==");
73                      new CSSCompiler().compile();
74                  } else if ("js".equalsIgnoreCase(_type)) {
75                      AbstractRest.LOG.info("==Compiling Javascript==");
76                      new JavaScriptCompiler().compile();
77                  } else if ("wiki".equalsIgnoreCase(_type)) {
78                      AbstractRest.LOG.info("==Compiling Wiki==");
79                      new WikiCompiler().compile();
80                  } else if ("jasper".equalsIgnoreCase(_type)) {
81                      AbstractRest.LOG.info("==Compiling JasperReports==");
82                      new JasperReportCompiler(getClassPathElements()).compile();
83                  }
84                  success = true;
85                  AbstractRest.LOG.info("===Ending Compiler via REST===");
86              }
87          } catch (final InstallationException e) {
88              AbstractRest.LOG.error("InstallationException", e);
89          } catch (final EFapsException e) {
90              AbstractRest.LOG.error("EFapsException", e);
91          }
92          return success ? Response.ok().build() : Response.noContent().build();
93      }
94  
95      /**
96       * @return list of classpath elements
97       * @throws EFapsException on error
98       */
99      public static List<String> getClassPathElements()
100         throws EFapsException
101     {
102         final List<String> ret = new ArrayList<String>();
103         final SystemConfiguration config = EFapsSystemConfiguration.get();
104         final String paths = config.getAttributeValue(KernelSettings.CLASSPATHS);
105 
106         if (paths != null) {
107             final File folder = new File(paths);
108             File[] files = null;
109             if (folder.isDirectory()) {
110                 files = folder.listFiles(new FilenameFilter() {
111 
112                     @Override
113                     public boolean accept(final File _dir,
114                                           final String _name)
115                     {
116                         final boolean ret;
117                         if (new File(_dir, _name).isDirectory()) {
118                             ret = false;
119                         } else {
120                             final String name = _name.toLowerCase();
121                             ret = name.endsWith(".jar");
122                         }
123                         return ret;
124                     }
125                 });
126                 for (final File file : files) {
127                     ret.add(file.getAbsolutePath());
128                 }
129             }
130         } else {
131             AbstractRest.LOG.info("==ClassPath is not configurated==");
132         }
133         return ret;
134     }
135 }