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.SortedSet;
20 import java.util.TreeSet;
21
22 import COM.FutureTense.Interfaces.ICS;
23 import COM.FutureTense.Interfaces.IList;
24
25 import com.fatwire.assetapi.data.AssetData;
26 import com.fatwire.gst.foundation.facade.assetapi.AssetDataUtils;
27 import com.fatwire.gst.foundation.facade.assetapi.AssetIdUtils;
28 import com.fatwire.gst.foundation.facade.assetapi.AttributeDataUtils;
29 import com.fatwire.gst.foundation.facade.runtag.asset.AssetList;
30 import com.fatwire.gst.foundation.facade.sql.IListIterable;
31 import com.fatwire.gst.foundation.facade.sql.Row;
32 import com.fatwire.gst.foundation.wra.VanityAsset;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37
38
39
40
41
42
43 public final class AssetApiVirtualWebrootDao implements VirtualWebrootDao {
44
45 private static final Log LOG = LogFactory.getLog(AssetApiVirtualWebrootDao.class.getName());
46
47 private final ICS ics;
48
49 public AssetApiVirtualWebrootDao(ICS ics) {
50 this.ics = ics;
51 }
52
53 public VirtualWebroot getVirtualWebroot(long cid) {
54 String sCid = Long.toString(cid);
55 if (LOG.isTraceEnabled())
56 LOG.trace("Loading virtual webroot data for for GSTVirtualWebroot:" + sCid);
57 AssetData ad = AssetDataUtils.getAssetData(ics, AssetIdUtils.createAssetId("GSTVirtualWebroot", sCid), "master_vwebroot", "env_vwebroot",
58 "env_name");
59 return new VWebrootBeanImpl(cid, AttributeDataUtils.getWithFallback(ad, "master_vwebroot"),
60 AttributeDataUtils.getWithFallback(ad, "env_vwebroot"), AttributeDataUtils.getWithFallback(ad,
61 "env_name"));
62
63 }
64
65
66
67
68
69
70 public SortedSet<VirtualWebroot> getAllVirtualWebroots() {
71 AssetList al = new AssetList();
72 al.setExcludeVoided(true);
73 al.setList("pr-out");
74 al.setType("GSTVirtualWebroot");
75 ics.RegisterList("pr-out", null);
76 al.execute(ics);
77 IList ilist = ics.GetList("pr-out");
78 ics.RegisterList("pr-out", null);
79 if (ilist == null)
80 throw new IllegalStateException("No GSTVirtualWebroots are registered");
81
82 SortedSet<VirtualWebroot> result = new TreeSet<VirtualWebroot>(new UrlInfoComparator());
83 for (Row r : new IListIterable(ilist)) {
84 result.add(getVirtualWebroot(r.getLong("id")));
85 }
86 return result;
87 }
88
89
90
91
92
93
94
95 public String getVirtualWebrootEnvironment() {
96 String environmentName = System.getProperty("com.fatwire.gst.foundation.env-name", null);
97
98
99 if (environmentName != null) {
100 environmentName = environmentName.trim();
101 if (environmentName.length() == 0)
102 environmentName = null;
103 }
104
105 if (environmentName == null) {
106
107 environmentName = ics.GetProperty("com.fatwire.gst.foundation.env-name");
108
109 if (environmentName != null) {
110 environmentName = environmentName.trim();
111 if (environmentName.length() == 0)
112 environmentName = null;
113 }
114 }
115 if (environmentName == null)
116 LOG.debug("Virtual webroot environment is not configured.");
117 return environmentName;
118 }
119
120
121
122
123
124
125
126
127
128 public VirtualWebroot lookupVirtualWebrootForAsset(VanityAsset wra) {
129 if (LOG.isDebugEnabled())
130 LOG.debug("Looking up virtual webroot for WRA " + wra.getId());
131 String wraPath = wra.getPath();
132 return lookupVirtualWebrootForUri(wraPath);
133 }
134
135 public VirtualWebroot lookupVirtualWebrootForUri(String wraPath) {
136 if (wraPath == null) {
137 LOG.trace("WRA does not have a path set - cannot locate virtual webroot");
138 return null;
139 }
140 String env = getVirtualWebrootEnvironment();
141 if (env == null)
142 return null;
143 for (VirtualWebroot vw : getAllVirtualWebroots()) {
144
145
146
147
148
149 if (env.equals(vw.getEnvironmentName()) && wraPath.startsWith(vw.getMasterVirtualWebroot())) {
150 return vw;
151 }
152 }
153 return null;
154 }
155
156
157
158
159
160
161 public static class UrlInfoComparator implements Comparator<VirtualWebroot> {
162
163 public int compare(VirtualWebroot o1, VirtualWebroot o2) {
164 int i = -o1.getMasterVirtualWebroot().compareTo(o2.getMasterVirtualWebroot());
165 if (i == 0) {
166 int j = -o1.getEnvironmentName().compareTo(o2.getEnvironmentName());
167 if (j == 0) {
168 int k = -o1.getEnvironmentVirtualWebroot().compareTo(o2.getEnvironmentVirtualWebroot());
169 if (k == 0) {
170 return (int) (o1.getId().getId() - o2.getId().getId());
171 }
172 return k;
173 }
174 return j;
175 }
176 return i;
177 }
178 }
179 }