KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > controllers > kernel > impl > simple > RepositoryDeliveryController


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.deliver.controllers.kernel.impl.simple;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 import org.apache.log4j.Logger;
34 import org.exolab.castor.jdo.Database;
35 import org.exolab.castor.jdo.OQLQuery;
36 import org.exolab.castor.jdo.QueryResults;
37 import org.infoglue.cms.entities.management.Repository;
38 import org.infoglue.cms.entities.management.RepositoryVO;
39 import org.infoglue.cms.exception.SystemException;
40 import org.infoglue.deliver.util.CacheController;
41 import org.infoglue.deliver.util.NullObject;
42
43 import com.opensymphony.module.propertyset.PropertySet;
44 import com.opensymphony.module.propertyset.PropertySetManager;
45
46 public class RepositoryDeliveryController extends BaseDeliveryController
47 {
48     private final static Logger logger = Logger.getLogger(RepositoryDeliveryController.class.getName());
49
50     /**
51      * Private constructor to enforce factory-use
52      */

53     
54     private RepositoryDeliveryController()
55     {
56     }
57     
58     /**
59      * Factory method
60      */

61     
62     public static RepositoryDeliveryController getRepositoryDeliveryController()
63     {
64         return new RepositoryDeliveryController();
65     }
66     
67
68     /**
69      * This method returns the master repository.
70      */

71     
72     public RepositoryVO getMasterRepository(Database db) throws SystemException, Exception JavaDoc
73     {
74         RepositoryVO repositoryVO = (RepositoryVO)CacheController.getCachedObject("masterRepository", "masterRepository");
75         if(repositoryVO != null)
76             return repositoryVO;
77         
78         OQLQuery oql = db.getOQLQuery( "SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RepositoryImpl r ORDER BY r.repositoryId");
79         
80         QueryResults results = oql.execute(Database.ReadOnly);
81         
82         if (results.hasMore())
83         {
84             Repository repository = (Repository)results.next();
85             repositoryVO = repository.getValueObject();
86         }
87
88         results.close();
89         oql.close();
90
91         if(repositoryVO != null)
92             CacheController.cacheObject("masterRepository", "masterRepository", repositoryVO);
93         
94         return repositoryVO;
95     }
96     
97     public List JavaDoc getRepositoryVOListFromServerName(Database db, String JavaDoc serverName, String JavaDoc portNumber, String JavaDoc repositoryName) throws SystemException, Exception JavaDoc
98     {
99         List JavaDoc repositories = new ArrayList JavaDoc();
100         
101         List JavaDoc cachedRepositories = (List JavaDoc)CacheController.getCachedObject("masterRepository", "allDNSRepositories");
102         if(cachedRepositories == null)
103         {
104             cachedRepositories = new ArrayList JavaDoc();
105             
106             OQLQuery oql = db.getOQLQuery( "SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RepositoryImpl r WHERE is_defined(r.dnsName)");
107             QueryResults results = oql.execute(Database.ReadOnly);
108         
109             while (results.hasMore())
110             {
111                 Repository repository = (Repository) results.next();
112                 cachedRepositories.add(repository.getValueObject());
113             }
114             
115             results.close();
116             oql.close();
117
118             if(cachedRepositories.size() > 0)
119                 CacheController.cacheObject("masterRepository", "allDNSRepositories", cachedRepositories);
120         }
121         
122         Iterator JavaDoc repositoriesIterator = cachedRepositories.iterator();
123         while (repositoriesIterator.hasNext())
124         {
125             RepositoryVO repositoryVO = (RepositoryVO) repositoriesIterator.next();
126             logger.info("repository:" + repositoryVO.getDnsName());
127             String JavaDoc[] dnsNames = splitStrings(repositoryVO.getDnsName());
128             logger.info("dnsNames:" + dnsNames);
129             for (int i=0;i<dnsNames.length;i++)
130             {
131                 logger.info("dnsNames[i]:" + dnsNames[i]);
132                 String JavaDoc dnsName = dnsNames[i];
133                 int protocolIndex = dnsName.indexOf("://");
134                 if(protocolIndex > -1)
135                     dnsName = dnsName.substring(protocolIndex + 3);
136                 
137                 logger.info("Matching only server name - removed protocol if there:" + dnsName);
138                 
139                 if((dnsName.indexOf(":") == -1 && dnsName.indexOf(serverName) == 0) || dnsName.indexOf(serverName + ":" + portNumber) == 0)
140                 {
141                     if(repositoryName != null && repositoryName.length() > 0)
142                     {
143                         logger.info("Has to check repositoryName also:" + repositoryName);
144                         if(repositoryVO.getName().equalsIgnoreCase(repositoryName))
145                             repositories.add(repositoryVO);
146                     }
147                     else
148                     {
149                         repositories.add(repositoryVO);
150                     }
151                 }
152             }
153         }
154         
155         return repositories;
156     }
157
158 /*
159     public List getRepositoriesFromServerName(Database db, String serverName, String portNumber, String repositoryName) throws SystemException, Exception
160     {
161         List repositories = new ArrayList();
162         
163         OQLQuery oql = db.getOQLQuery( "SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RepositoryImpl r WHERE is_defined(r.dnsName)");
164         QueryResults results = oql.execute(Database.ReadOnly);
165         while (results.hasMore())
166         {
167             Repository repository = (Repository) results.next();
168             logger.info("repository:" + repository.getDnsName());
169             String[] dnsNames = splitStrings(repository.getDnsName());
170             logger.info("dnsNames:" + dnsNames);
171             for (int i=0;i<dnsNames.length;i++)
172             {
173                 logger.info("dnsNames[i]:" + dnsNames[i]);
174                 String dnsName = dnsNames[i];
175                 int protocolIndex = dnsName.indexOf("://");
176                 if(protocolIndex > -1)
177                     dnsName = dnsName.substring(protocolIndex + 3);
178                 
179                 logger.info("Matching only server name - removed protocol if there:" + dnsName);
180                 
181                 if((dnsName.indexOf(":") == -1 && dnsName.indexOf(serverName) == 0) || dnsName.indexOf(serverName + ":" + portNumber) == 0)
182                 {
183                     if(repositoryName != null && repositoryName.length() > 0)
184                     {
185                         logger.info("Has to check repositoryName also:" + repositoryName);
186                         if(repository.getValueObject().getName().equalsIgnoreCase(repositoryName))
187                             repositories.add(repository.getValueObject());
188                     }
189                     else
190                     {
191                         repositories.add(repository.getValueObject());
192                     }
193                 }
194             }
195         }
196         
197         return repositories;
198     }
199 */

200     
201     private String JavaDoc[] splitStrings(String JavaDoc str)
202     {
203         List JavaDoc list = new ArrayList JavaDoc();
204         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(str, ",");
205         while (st.hasMoreTokens())
206         {
207             String JavaDoc token = st.nextToken().trim();
208             list.add(token);
209         }
210         
211         return (String JavaDoc[]) list.toArray(new String JavaDoc[0]);
212     }
213     
214     /**
215      * This method returns all the repositories.
216      */

217     
218     public List JavaDoc getRepositoryVOList(Database db) throws SystemException, Exception JavaDoc
219     {
220         List JavaDoc repositoryVOList = new ArrayList JavaDoc();
221         
222         OQLQuery oql = db.getOQLQuery( "SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RepositoryImpl r ORDER BY r.repositoryId");
223         
224         QueryResults results = oql.execute(Database.ReadOnly);
225         
226         if (results.hasMore())
227         {
228             Repository repository = (Repository)results.next();
229             RepositoryVO repositoryVO = repository.getValueObject();
230             repositoryVOList.add(repositoryVO);
231         }
232
233         results.close();
234         oql.close();
235
236         return repositoryVOList;
237     }
238
239     /**
240      * This method fetches a property for a repository.
241      */

242     
243     public String JavaDoc getPropertyValue(Integer JavaDoc repositoryId, String JavaDoc propertyName)
244     {
245         String JavaDoc key = "parentRepository_" + repositoryId + "_" + propertyName;
246         logger.info("key:" + key);
247         Object JavaDoc object = CacheController.getCachedObject("parentRepository", key);
248         
249         if(object instanceof NullObject)
250         {
251             logger.info("There was an cached property but it was null:" + object);
252             return null;
253         }
254         else if(object != null)
255         {
256             logger.info("There was an cached property:" + object);
257             return (String JavaDoc)object;
258         }
259         
260         String JavaDoc propertyValue = null;
261         
262         Map JavaDoc args = new HashMap JavaDoc();
263         args.put("globalKey", "infoglue");
264         PropertySet ps = PropertySetManager.getInstance("jdbc", args);
265         
266         propertyValue = ps.getString("repository_" + repositoryId + "_" + propertyName);
267         logger.info("propertyValue:" + propertyValue);
268         if(propertyValue != null)
269             CacheController.cacheObject("parentRepository", key, propertyValue);
270         else
271             CacheController.cacheObject("parentRepository", key, new NullObject());
272             
273         return propertyValue;
274     }
275
276 }
Popular Tags