1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
37
38
39
40
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
63
64
65
66
67
68
69
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
85
86
87
88
89 public String getVirtualWebrootEnvironment() {
90 String environmentName = System.getProperty("com.fatwire.gst.foundation.env-name", null);
91
92
93 if (environmentName != null) {
94 environmentName = environmentName.trim();
95 if (environmentName.length() == 0)
96 environmentName = null;
97 }
98
99 if (environmentName == null) {
100
101 environmentName = ics.GetProperty("com.fatwire.gst.foundation.env-name");
102
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
116
117
118
119
120
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
139
140
141
142
143 if (env.equals(vw.getEnvironmentName()) && wraPath.startsWith(vw.getMasterVirtualWebroot())) {
144 return vw;
145 }
146 }
147 return null;
148 }
149
150
151
152
153
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 }