View Javadoc
1   /**
2    * This file Copyright (c) 2016 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.resourceloader.classpath.service.impl.devmode;
35  
36  import info.magnolia.resourceloader.classpath.hierarchy.ClasspathFile;
37  import info.magnolia.resourceloader.classpath.service.impl.URLConnectionUtil;
38  
39  import java.io.IOException;
40  import java.io.InputStream;
41  import java.net.URL;
42  import java.net.URLConnection;
43  
44  import org.reflections.util.ClasspathHelper;
45  import org.reflections.vfs.Vfs;
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  import com.google.common.base.Objects;
50  
51  /**
52   * {@link ClasspathFile} implementation oriented onto <strong>development application mode</strong>.
53   *
54   * <p>Communicates to the actual file via parent jar URL and {@link Vfs.File} descriptor.</p>
55   */
56  public class DevelopmentModeClasspathFile implements ClasspathFile {
57  
58      private static final Logger log = LoggerFactory.getLogger(DevelopmentModeClasspathFile.class);
59  
60      private URL classpathLocation;
61      private Vfs.File jarFile;
62  
63      public DevelopmentModeClasspathFile(URL classpathLocation, Vfs.File jarFile) {
64          this.classpathLocation = classpathLocation;
65          this.jarFile = jarFile;
66      }
67  
68      @Override
69      public String getName() {
70          return jarFile.getName();
71      }
72  
73      @Override
74      public String getAbsolutePath() {
75          return "/" + jarFile.getRelativePath();
76      }
77  
78      @Override
79      public InputStream openStream() throws IOException {
80          return jarFile.openInputStream();
81      }
82  
83      @Override
84      public long getLastModified() {
85          URLConnection urlConnection = null;
86          try {
87              // If the resource file was in classpath from the very beginning - it'll probably be resolvable via
88              // context class loader and we might get more precise last modification date.
89              final URL resourceUrl = ClasspathHelper.contextClassLoader().getResource(jarFile.getRelativePath());
90              if (resourceUrl != null) {
91                  urlConnection = resourceUrl.openConnection();
92              } else {
93                  // Since the current strategy of resource content resolution is to look inside the jar contents
94                  // the changed resources have the same last modification date as the jar which hosts them
95                  urlConnection = classpathLocation.openConnection();
96              }
97  
98              return urlConnection.getLastModified();
99          } catch (IOException e) {
100             log.warn("Failed to check the last modification date of a resource at {}, problems with establishing the connection with the jar file containing the resource [{}] resource: {}. Returning -1", getAbsolutePath(), classpathLocation, e.getMessage());
101             return -1;
102         } finally {
103             URLConnectionUtil.closeURLConnection(urlConnection);
104         }
105     }
106 
107     @Override
108     public boolean equals(Object o) {
109         if (this == o) return true;
110         if (o == null || getClass() != o.getClass()) return false;
111 
112         DevelopmentModeClasspathFile that = (DevelopmentModeClasspathFile) o;
113 
114         return Objects.equal(getAbsolutePath(), that.getAbsolutePath());
115     }
116 
117     @Override
118     public int hashCode() {
119         return Objects.hashCode(getAbsolutePath());
120     }
121 }