KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > ServerNodeController


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.cms.controllers.kernel.impl.simple;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35
36 import org.apache.log4j.Logger;
37 import org.exolab.castor.jdo.Database;
38 import org.exolab.castor.jdo.OQLQuery;
39 import org.exolab.castor.jdo.QueryResults;
40 import org.infoglue.cms.entities.kernel.BaseEntityVO;
41 import org.infoglue.cms.entities.management.ServerNode;
42 import org.infoglue.cms.entities.management.ServerNodeVO;
43 import org.infoglue.cms.entities.management.impl.simple.ServerNodeImpl;
44 import org.infoglue.cms.exception.Bug;
45 import org.infoglue.cms.exception.ConstraintException;
46 import org.infoglue.cms.exception.SystemException;
47 import org.infoglue.cms.security.InfoGlueAuthenticationFilter;
48 import org.infoglue.cms.security.InfoGluePrincipal;
49 import org.infoglue.cms.util.CmsPropertyHandler;
50 import org.infoglue.cms.util.NotificationMessage;
51 import org.infoglue.cms.util.RemoteCacheUpdater;
52 import org.infoglue.deliver.util.CacheController;
53
54 import com.opensymphony.module.propertyset.PropertySet;
55 import com.opensymphony.module.propertyset.PropertySetManager;
56
57 public class ServerNodeController extends BaseController
58 {
59     private final static Logger logger = Logger.getLogger(ServerNodeController.class.getName());
60
61     private String JavaDoc useUpdateSecurity = CmsPropertyHandler.getUseUpdateSecurity();
62     
63     /**
64      * Factory method
65      */

66
67     public static ServerNodeController getController()
68     {
69         return new ServerNodeController();
70     }
71     
72     public void initialize()
73     {
74     }
75     
76     /**
77      * This method creates a serverNode
78      *
79      * @param vo
80      * @return
81      * @throws ConstraintException
82      * @throws SystemException
83      */

84     public ServerNodeVO create(ServerNodeVO vo) throws ConstraintException, SystemException
85     {
86         ServerNode ent = new ServerNodeImpl();
87         ent.setValueObject(vo);
88         ent = (ServerNode) createEntity(ent);
89         return ent.getValueObject();
90     }
91     
92     public ServerNodeVO update(ServerNodeVO vo) throws ConstraintException, SystemException
93     {
94         return (ServerNodeVO) updateEntity(ServerNodeImpl.class, (BaseEntityVO) vo);
95     }
96         
97     // Singe object
98
public ServerNode getServerNodeWithId(Integer JavaDoc id, Database db) throws SystemException, Bug
99     {
100         return (ServerNode) getObjectWithId(ServerNodeImpl.class, id, db);
101     }
102
103     public ServerNodeVO getServerNodeVOWithId(Integer JavaDoc serverNodeId) throws ConstraintException, SystemException, Bug
104     {
105         return (ServerNodeVO) getVOWithId(ServerNodeImpl.class, serverNodeId);
106     }
107     
108     
109     /**
110      * Returns the ServerNodeVO with the given name.
111      *
112      * @param name
113      * @return
114      * @throws SystemException
115      * @throws Bug
116      */

117     
118     public ServerNodeVO getServerNodeVOWithName(String JavaDoc name) throws SystemException, Bug
119     {
120         ServerNodeVO serverNodeVO = null;
121         
122         Database db = CastorDatabaseService.getDatabase();
123
124         try
125         {
126             beginTransaction(db);
127
128             ServerNode serverNode = getServerNodeWithName(name, db);
129             if(serverNode != null)
130                 serverNodeVO = serverNode.getValueObject();
131             
132             commitTransaction(db);
133         }
134         catch (Exception JavaDoc e)
135         {
136             logger.info("An error occurred so we should not complete the transaction:" + e);
137             rollbackTransaction(db);
138             throw new SystemException(e.getMessage());
139         }
140         
141         return serverNodeVO;
142     }
143     
144     /**
145      * Returns the ServerNode with the given name fetched within a given transaction.
146      *
147      * @param name
148      * @param db
149      * @return
150      * @throws SystemException
151      * @throws Bug
152      */

153
154     public ServerNode getServerNodeWithName(String JavaDoc name, Database db) throws SystemException, Bug
155     {
156         ServerNode serverNode = null;
157         
158         try
159         {
160             OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.ServerNodeImpl f WHERE f.name = $1");
161             oql.bind(name);
162             
163             QueryResults results = oql.execute();
164             this.logger.info("Fetching entity in read/write mode" + name);
165
166             if (results.hasMore())
167             {
168                 serverNode = (ServerNode)results.next();
169             }
170             
171             results.close();
172             oql.close();
173         }
174         catch(Exception JavaDoc e)
175         {
176             throw new SystemException("An error occurred when we tried to fetch a named serverNode. Reason:" + e.getMessage(), e);
177         }
178         
179         return serverNode;
180     }
181
182     /**
183      * This method can be used by actions and use-case-controllers that only need to have simple access to the
184      * functionality. They don't get the transaction-safety but probably just wants to show the info.
185      */

186     
187     public List JavaDoc getServerNodeVOList() throws SystemException, Bug
188     {
189         /*
190         String key = "serverNodeVOList";
191         logger.info("key:" + key);
192         List cachedServerNodeVOList = (List)CacheController.getCachedObject("serverNodeCache", key);
193         if(cachedServerNodeVOList != null)
194         {
195             logger.info("There was an cached authorization:" + cachedServerNodeVOList.size());
196             return cachedServerNodeVOList;
197         }
198         */

199         
200         List JavaDoc serverNodeVOList = getAllVOObjects(ServerNodeImpl.class, "serverNodeId");
201
202         //CacheController.cacheObject("serverNodeCache", key, serverNodeVOList);
203

204         return serverNodeVOList;
205     }
206     
207
208     public void delete(ServerNodeVO serverNodeVO, InfoGluePrincipal infoGluePrincipal) throws ConstraintException, SystemException
209     {
210         Integer JavaDoc serverNodeId = serverNodeVO.getId();
211         
212         deleteEntity(ServerNodeImpl.class, serverNodeVO.getId());
213
214         Map JavaDoc args = new HashMap JavaDoc();
215         args.put("globalKey", "infoglue");
216         PropertySet ps = PropertySetManager.getInstance("jdbc", args);
217         
218         Collection JavaDoc keys = ps.getKeys();
219         Iterator JavaDoc keysIterator = keys.iterator();
220         while(keysIterator.hasNext())
221         {
222             String JavaDoc key = (String JavaDoc)keysIterator.next();
223             //System.out.println("key:" + key);
224
if(key.indexOf("serverNode_" + serverNodeId + "_") > -1)
225                 ps.remove(key);
226         }
227         
228         try
229         {
230             CacheController.clearServerNodeProperty();
231             InfoGlueAuthenticationFilter.initializeCMSProperties();
232         }
233         catch (SystemException e)
234         {
235             e.printStackTrace();
236         }
237
238         NotificationMessage notificationMessage = new NotificationMessage("ViewServerNodePropertiesAction.doSave():", "ServerNodeProperties", infoGluePrincipal.getName(), NotificationMessage.SYSTEM, "0", "ServerNodeProperties");
239         RemoteCacheUpdater.getSystemNotificationMessages().add(notificationMessage);
240     }
241     
242     
243     public List JavaDoc getAllowedAdminIPList()
244     {
245         Map JavaDoc args = new HashMap JavaDoc();
246         args.put("globalKey", "infoglue");
247         PropertySet ps = PropertySetManager.getInstance("jdbc", args);
248         
249         String JavaDoc allowedAdminIP = ps.getString("allowedAdminIP");
250         if(allowedAdminIP != null)
251             return Arrays.asList(allowedAdminIP.split(","));
252         else
253             return new ArrayList JavaDoc();
254     }
255     
256     /**
257      * This method return if the caller has access to the semi admin services.
258      * @param request
259      * @return
260      */

261
262     public boolean getIsIPAllowed(HttpServletRequest JavaDoc request)
263     {
264         boolean isIPAllowed = false;
265
266         //System.out.println("useUpdateSecurity:" + useUpdateSecurity);
267
if(useUpdateSecurity != null && useUpdateSecurity.equals("true"))
268         {
269             String JavaDoc remoteIP = request.getRemoteAddr();
270             //System.out.println("remoteIP:" + remoteIP);
271
if(remoteIP.equals("127.0.0.1"))
272             {
273                 isIPAllowed = true;
274             }
275             else
276             {
277                 List JavaDoc allowedAdminIPList = ServerNodeController.getController().getAllowedAdminIPList();
278                 Iterator JavaDoc i = allowedAdminIPList.iterator();
279                 while(i.hasNext())
280                 {
281                     String JavaDoc allowedIP = (String JavaDoc)i.next();
282                     if(!allowedIP.trim().equals(""))
283                     {
284                         //System.out.println("allowedIP:" + allowedIP);
285
int index = allowedIP.indexOf(".*");
286                         if(index > -1)
287                             allowedIP = allowedIP.substring(0, index);
288                         //System.out.println("allowedIP:" + allowedIP);
289

290                         if(remoteIP.startsWith(allowedIP))
291                         {
292                             isIPAllowed = true;
293                             break;
294                         }
295                     }
296                 }
297             }
298         }
299         else
300             isIPAllowed = true;
301         
302         return isIPAllowed;
303     }
304     
305     public String JavaDoc getAllowedAdminIP()
306     {
307         Map JavaDoc args = new HashMap JavaDoc();
308         args.put("globalKey", "infoglue");
309         PropertySet ps = PropertySetManager.getInstance("jdbc", args);
310         
311         String JavaDoc allowedAdminIP = ps.getString("allowedAdminIP");
312
313         return allowedAdminIP;
314     }
315
316     public void setAllowedAdminIP(String JavaDoc allowedAdminIP)
317     {
318         Map JavaDoc args = new HashMap JavaDoc();
319         args.put("globalKey", "infoglue");
320         PropertySet ps = PropertySetManager.getInstance("jdbc", args);
321         
322         ps.setString("allowedAdminIP", allowedAdminIP);
323     }
324
325     /**
326      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
327      * is handling.
328      */

329
330     public BaseEntityVO getNewVO()
331     {
332         return new ServerNodeVO();
333     }
334
335 }
336  
337
Popular Tags