View Javadoc

1   /*
2    * Copyright 2010 FatWire Corporation. All Rights Reserved.
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  package com.fatwire.gst.foundation.vwebroot;
17  
18  import java.util.Comparator;
19  import java.util.Map;
20  import java.util.SortedSet;
21  import java.util.TreeSet;
22  
23  import COM.FutureTense.Interfaces.ICS;
24  
25  import com.fatwire.assetapi.data.AssetId;
26  import com.fatwire.gst.foundation.facade.assetapi.DirectSqlAccessTools;
27  import com.fatwire.gst.foundation.facade.sql.Row;
28  import com.fatwire.gst.foundation.facade.sql.SqlHelper;
29  import com.fatwire.gst.foundation.wra.VanityAsset;
30  import com.openmarket.xcelerate.asset.AssetIdImpl;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  /**
36   * Backdoor implementation of VirtualWebrootDao that does not utilize any Asset
37   * APIs. This class should be used sparingly and may result in some
38   * dependencies, that would ordinarily be recorded, being skipped.
39   * <p/>
40   * User: Tony Field Date: 2011-05-06
41   */
42  public class VirtualWebrootApiBypassDao implements VirtualWebrootDao {
43      private static final Log LOG = LogFactory.getLog(VirtualWebrootApiBypassDao.class.getName());
44  
45      private final ICS ics;
46      private final DirectSqlAccessTools directSqlAccessTools;
47  
48      public VirtualWebrootApiBypassDao(ICS ics) {
49          this.ics = ics;
50          this.directSqlAccessTools = new DirectSqlAccessTools(ics);
51      }
52  
53      public VirtualWebroot getVirtualWebroot(long cid) {
54          if (LOG.isTraceEnabled())
55              LOG.trace("Loading virtual webroot data for for GSTVirtualWebroot:" + cid);
56          AssetId id = new AssetIdImpl("GSTVirtualWebroot", cid);
57          Map<String, String> m = directSqlAccessTools.getFlexAttributeValues(id, "master_vwebroot", "env_vwebroot",
58                  "env_name");
59          return new VWebrootBeanImpl(cid, m.get("master_vwebroot"), m.get("env_vwebroot"), m.get("env_name"));
60      }
61  
62      // private static final PreparedStmt ALL_VW = new
63      // PreparedStmt("SELECT id from GSTVirtualWebroot WHERE status != 'VO'",
64      // Collections.singletonList("GSTVirtualWebroot"));
65  
66      /**
67       * Get all of the virtual webroots, sorted by URL length.
68       * 
69       * @return list of virtual webroots
70       */
71      public SortedSet<VirtualWebroot> getAllVirtualWebroots() {
72  
73          SortedSet<VirtualWebroot> result = new TreeSet<VirtualWebroot>(new UrlInfoComparator());
74          for (Row r : SqlHelper
75                  .select(ics, "GSTVirtualWebroot", "SELECT id from GSTVirtualWebroot WHERE status != 'VO'")) {
76              result.add(getVirtualWebroot(r.getLong("id")));
77          }
78          if (result.size() == 0)
79              throw new IllegalStateException("No GSTVirtualWebroots are registered");
80          return result;
81      }
82  
83      /**
84       * Get the current virtual webroot environment as defined by the
85       * configuration properties. Null indicates that none is configured.
86       * 
87       * @return virtual webroot environment or null if not set.
88       */
89      public String getVirtualWebrootEnvironment() {
90          String environmentName = System.getProperty("com.fatwire.gst.foundation.env-name", null);
91  
92          // avoid configuration problem trickery
93          if (environmentName != null) {
94              environmentName = environmentName.trim();
95              if (environmentName.length() == 0)
96                  environmentName = null;
97          }
98  
99          if (environmentName == null) {
100             // allow user to have accidentally mis-configured things
101             environmentName = ics.GetProperty("com.fatwire.gst.foundation.env-name");
102             // avoid configuration problem trickery
103             if (environmentName != null) {
104                 environmentName = environmentName.trim();
105                 if (environmentName.length() == 0)
106                     environmentName = null;
107             }
108         }
109         if (environmentName == null)
110             LOG.debug("Virtual webroot environment is not configured.");
111         return environmentName;
112     }
113 
114     /**
115      * Look up and return the VirtualWebroot corresponding to the specified
116      * VanityAsset, for the current environment. If the current environment is
117      * not configured, no match can be found.
118      * 
119      * @param wra web-referenceable asset
120      * @return matching VirtualWebroot or null if no match is found.
121      */
122     public VirtualWebroot lookupVirtualWebrootForAsset(VanityAsset wra) {
123         if (LOG.isDebugEnabled())
124             LOG.debug("Looking up virtual webroot for WRA " + wra.getId());
125         String wraPath = wra.getPath();
126         return lookupVirtualWebrootForUri(wraPath);
127     }
128 
129     public VirtualWebroot lookupVirtualWebrootForUri(String wraPath) {
130         if (wraPath == null) {
131             LOG.trace("WRA does ont have a path set - cannot locate virtual webroot");
132             return null;
133         }
134         String env = getVirtualWebrootEnvironment();
135         if (env == null)
136             return null;
137         for (VirtualWebroot vw : getAllVirtualWebroots()) {
138             // find longest first one that is found in the prefix of path. that
139             // is virtual-webroot
140             // the path in the asset must start with the MASTER virtual webroot
141             // for this to work. This could
142             // be loosened up but there is no real reason to right now.
143             if (env.equals(vw.getEnvironmentName()) && wraPath.startsWith(vw.getMasterVirtualWebroot())) {
144                 return vw;
145             }
146         }
147         return null; // no match
148     }
149 
150     /**
151      * Comparator that compares virtual webroots by webroot. Uses
152      * reverse-natural ordering to ensure that overlapping virtual webroots
153      * resolve properly.
154      */
155     public static class UrlInfoComparator implements Comparator<VirtualWebroot> {
156 
157         public int compare(VirtualWebroot o1, VirtualWebroot o2) {
158             int i = -o1.getMasterVirtualWebroot().compareTo(o2.getMasterVirtualWebroot());
159             if (i == 0) {
160                 int j = -o1.getEnvironmentName().compareTo(o2.getEnvironmentName());
161                 if (j == 0) {
162                     int k = -o1.getEnvironmentVirtualWebroot().compareTo(o2.getEnvironmentVirtualWebroot());
163                     if (k == 0) {
164                         return (int) (o1.getId().getId() - o2.getId().getId());
165                     }
166                     return k;
167                 }
168                 return j;
169             }
170             return i;
171         }
172     }
173 }