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.url.db;
17  
18  import java.util.Collections;
19  
20  import COM.FutureTense.Interfaces.FTValList;
21  import COM.FutureTense.Interfaces.ICS;
22  import COM.FutureTense.Util.ftMessage;
23  
24  import com.fatwire.assetapi.data.AssetId;
25  import com.fatwire.cs.core.db.PreparedStmt;
26  import com.fatwire.cs.core.db.StatementParam;
27  import com.fatwire.cs.core.db.Util;
28  import com.fatwire.gst.foundation.CSRuntimeException;
29  import com.fatwire.gst.foundation.controller.AssetIdWithSite;
30  import com.fatwire.gst.foundation.facade.logging.LogUtil;
31  import com.fatwire.gst.foundation.facade.runtag.asset.FilterAssetsByDate;
32  import com.fatwire.gst.foundation.facade.sql.Row;
33  import com.fatwire.gst.foundation.facade.sql.SqlHelper;
34  import com.fatwire.gst.foundation.facade.sql.table.TableColumn;
35  import com.fatwire.gst.foundation.facade.sql.table.TableColumn.Type;
36  import com.fatwire.gst.foundation.facade.sql.table.TableCreator;
37  import com.fatwire.gst.foundation.facade.sql.table.TableDef;
38  import com.fatwire.gst.foundation.url.WraPathTranslationService;
39  import com.fatwire.gst.foundation.vwebroot.VirtualWebroot;
40  import com.fatwire.gst.foundation.vwebroot.VirtualWebrootDao;
41  import com.fatwire.gst.foundation.wra.VanityAsset;
42  import com.fatwire.gst.foundation.wra.WraCoreFieldDao;
43  
44  import org.apache.commons.lang.StringUtils;
45  import org.apache.commons.logging.Log;
46  
47  /**
48   * WraPathTranslationService that is backed by the GSTUrlRegistry table.
49   * 
50   * @author Dolf.Dijkstra
51   * @since Jun 17, 2010
52   * @Deprecated use {@link UrlRegistry2}
53   */
54   @Deprecated
55  public class UrlRegistry implements WraPathTranslationService {
56  
57      private static final Log LOG = LogUtil.getLog(UrlRegistry.class);
58  
59      private final ICS ics;
60      private final WraCoreFieldDao wraDao;
61      private final VirtualWebrootDao vwDao;
62      private static final String URLREG_TABLE = "GSTUrlRegistry";
63      public static String TABLE_ACL_LIST = ""; // no ACLs because events are
64  
65      // anonymous
66  
67      /**
68       * 
69       * @param ics
70       * @param wraDao
71       * @param vwDao
72       * @deprecated
73       */
74     @Deprecated
75      public UrlRegistry(final ICS ics, final WraCoreFieldDao wraDao, final VirtualWebrootDao vwDao) {
76          this.ics = ics;
77          // Temporarily disable usage of asset APIs in this use case due to a bug
78          // in which asset listeners
79          // cause a deadlock when the asset API is used.
80          this.wraDao = wraDao;// WraCoreFieldApiBypassDao.getBackdoorInstance(ics);
81          this.vwDao = vwDao;// new VirtualWebrootApiBypassDao(ics);
82          // End temporary deadlock workaround
83      }
84  
85      public void install() {
86          final TableDef def = new TableDef(URLREG_TABLE, TABLE_ACL_LIST, ftMessage.objecttbl);
87  
88          def.addColumn(new TableColumn("id", Type.ccbigint, true).setNullable(false));
89          def.addColumn(new TableColumn("path", Type.ccvarchar).setLength(4000).setNullable(false));
90          def.addColumn(new TableColumn("assettype", Type.ccvarchar).setLength(255).setNullable(false));
91          def.addColumn(new TableColumn("assetid", Type.ccbigint).setNullable(false));
92          def.addColumn(new TableColumn("startdate", Type.ccdatetime).setNullable(true));
93          def.addColumn(new TableColumn("enddate", Type.ccdatetime).setNullable(true));
94          def.addColumn(new TableColumn("opt_vwebroot", Type.ccvarchar).setLength(255).setNullable(true));
95          def.addColumn(new TableColumn("opt_url_path", Type.ccvarchar).setLength(4000).setNullable(true));
96          def.addColumn(new TableColumn("opt_depth", Type.ccinteger).setNullable(true));
97          def.addColumn(new TableColumn("opt_site", Type.ccvarchar).setLength(255).setNullable(true));
98  
99          new TableCreator(ics).createTable(def);
100     }
101 
102     private static final PreparedStmt REGISTRY_SELECT = new PreparedStmt(
103             "SELECT assettype, assetid, startdate, enddate, opt_site FROM " + URLREG_TABLE
104                     + " WHERE opt_vwebroot=? AND opt_url_path=? ORDER BY startdate,enddate",
105             Collections.singletonList(URLREG_TABLE));
106     private static final PreparedStmt REGISTRY_SELECT_ID = new PreparedStmt("SELECT assettype, assetid FROM "
107             + URLREG_TABLE + " WHERE assettype=? AND assetid=?", Collections.singletonList(URLREG_TABLE));
108 
109     static {
110         REGISTRY_SELECT.setElement(0, URLREG_TABLE, "opt_vwebroot");
111         REGISTRY_SELECT.setElement(1, URLREG_TABLE, "opt_url_path");
112         REGISTRY_SELECT_ID.setElement(0, URLREG_TABLE, "assettype");
113         REGISTRY_SELECT_ID.setElement(1, URLREG_TABLE, "assetid");
114 
115     }
116 
117     @Override
118     public AssetIdWithSite resolveAsset(final String virtual_webroot, final String url_path) {
119         final StatementParam param = REGISTRY_SELECT.newParam();
120         param.setString(0, virtual_webroot);
121         param.setString(1, url_path);
122         for (final Row asset : SqlHelper.select(ics, REGISTRY_SELECT, param)) {
123             final String assettype = asset.getString("assettype");
124             final String assetid = asset.getString("assetid");
125             final AssetIdWithSite id = new AssetIdWithSite(assettype, Long.parseLong(assetid),
126                     asset.getString("opt_site"));
127             if (FilterAssetsByDate.isValidOnDate(ics, id, null)) {
128                 if (LOG.isDebugEnabled()) {
129                     LOG.debug("Resolved and validated effective date for asset " + id + " from virtual-webroot:"
130                             + virtual_webroot + " and url-path:" + url_path);
131                 }
132                 return id;
133             } else {
134                 if (LOG.isDebugEnabled()) {
135                     LOG.debug("Resolved asset "
136                             + id
137                             + " but it is not valid on the effective date as determined by the asset.filterassetsbydate tag.");
138                 }
139             }
140         }
141 
142         return null;
143     }
144 
145     @Override
146     public void addAsset(final AssetId asset) {
147         if (wraDao.isVanityAsset(asset)) {
148             if (LOG.isTraceEnabled()) {
149                 LOG.trace("Attempting to add WRA " + asset + " to url registry");
150             }
151             final VanityAsset wra = wraDao.getVanityWra(asset);
152             addAsset(wra);
153         } else {
154             if (LOG.isTraceEnabled()) {
155                 LOG.trace("Heard addAsset event for " + asset + " but since it is not a WRA we are ignoring it");
156             }
157         }
158     }
159 
160     private void addAsset(final VanityAsset wra) {
161         final AssetId asset = wra.getId();
162 
163         final VirtualWebroot vw = vwDao.lookupVirtualWebrootForAsset(wra);
164 
165         if (vw != null) {
166             final String vwebroot = vw.getEnvironmentVirtualWebroot();
167 
168             final String urlpath = wra.getPath().substring(vw.getMasterVirtualWebroot().length());
169             final int depth = StringUtils.countMatches(urlpath, "/");
170 
171             final String site = wraDao.resolveSite(asset.getType(), Long.toString(asset.getId()));
172 
173             final FTValList vl = new FTValList();
174             vl.setValString("ftcmd", "addrow");
175             vl.setValString("tablename", URLREG_TABLE);
176             vl.setValString("id", ics.genID(true));
177             vl.setValString("path", wra.getPath());
178             vl.setValString("assettype", asset.getType());
179             vl.setValString("assetid", Long.toString(asset.getId()));
180             if (wra.getStartDate() != null) {
181                 vl.setValString("startdate", Util.formatJdbcDate(wra.getStartDate()));
182             }
183             if (wra.getEndDate() != null) {
184                 vl.setValString("enddate", Util.formatJdbcDate(wra.getEndDate()));
185             }
186             vl.setValString("opt_vwebroot", vwebroot);
187             vl.setValString("opt_url_path", urlpath);
188             vl.setValString("opt_depth", Integer.toString(depth));
189             vl.setValString("opt_site", site);
190 
191             if (!ics.CatalogManager(vl) || ics.GetErrno() < 0) {
192                 throw new CSRuntimeException("Failure adding tag to tag registry", ics.GetErrno());
193             }
194 
195             if (LOG.isDebugEnabled()) {
196                 LOG.debug("Added WRA " + asset + " to url registry");
197             }
198 
199         } else {
200             if (LOG.isTraceEnabled()) {
201                 LOG.trace("Did not add WRA " + asset + " to url registry because no valid virtual webroot was found");
202             }
203         }
204 
205     }
206 
207     @Override
208     public void updateAsset(final AssetId id) {
209         // todo: low priority: optimize (assest api incache will mitigate the
210         // performance issue)
211 
212         final StatementParam param = REGISTRY_SELECT_ID.newParam();
213         param.setString(0, id.getType());
214         param.setLong(1, id.getId());
215         final Row row = SqlHelper.selectSingle(ics, REGISTRY_SELECT_ID, param);
216         if (row != null) {
217             deleteAsset(id);
218         }
219 
220         if (wraDao.isVanityAsset(id)) {
221             addAsset(id);
222         }
223     }
224 
225     @Override
226     public void deleteAsset(final AssetId id) {
227         if (LOG.isTraceEnabled()) {
228             LOG.trace("Attempting to delete asset " + id
229                     + " from url registry (it might not have been there but we must try anyway)");
230         }
231         SqlHelper.execute(ics, URLREG_TABLE, "DELETE FROM " + URLREG_TABLE + " WHERE assettype = '" + id.getType()
232                 + "' AND assetid = " + id.getId());
233         if (LOG.isDebugEnabled()) {
234             LOG.debug("Asset " + id + " was either never present or is now removed from url registry");
235         }
236     }
237 }