1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.Collection ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import javax.servlet.http.HttpServletRequest ; 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 useUpdateSecurity = CmsPropertyHandler.getUseUpdateSecurity(); 62 63 66 67 public static ServerNodeController getController() 68 { 69 return new ServerNodeController(); 70 } 71 72 public void initialize() 73 { 74 } 75 76 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 public ServerNode getServerNodeWithId(Integer id, Database db) throws SystemException, Bug 99 { 100 return (ServerNode) getObjectWithId(ServerNodeImpl.class, id, db); 101 } 102 103 public ServerNodeVO getServerNodeVOWithId(Integer serverNodeId) throws ConstraintException, SystemException, Bug 104 { 105 return (ServerNodeVO) getVOWithId(ServerNodeImpl.class, serverNodeId); 106 } 107 108 109 117 118 public ServerNodeVO getServerNodeVOWithName(String 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 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 153 154 public ServerNode getServerNodeWithName(String 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 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 186 187 public List getServerNodeVOList() throws SystemException, Bug 188 { 189 199 200 List serverNodeVOList = getAllVOObjects(ServerNodeImpl.class, "serverNodeId"); 201 202 204 return serverNodeVOList; 205 } 206 207 208 public void delete(ServerNodeVO serverNodeVO, InfoGluePrincipal infoGluePrincipal) throws ConstraintException, SystemException 209 { 210 Integer serverNodeId = serverNodeVO.getId(); 211 212 deleteEntity(ServerNodeImpl.class, serverNodeVO.getId()); 213 214 Map args = new HashMap (); 215 args.put("globalKey", "infoglue"); 216 PropertySet ps = PropertySetManager.getInstance("jdbc", args); 217 218 Collection keys = ps.getKeys(); 219 Iterator keysIterator = keys.iterator(); 220 while(keysIterator.hasNext()) 221 { 222 String key = (String )keysIterator.next(); 223 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 getAllowedAdminIPList() 244 { 245 Map args = new HashMap (); 246 args.put("globalKey", "infoglue"); 247 PropertySet ps = PropertySetManager.getInstance("jdbc", args); 248 249 String allowedAdminIP = ps.getString("allowedAdminIP"); 250 if(allowedAdminIP != null) 251 return Arrays.asList(allowedAdminIP.split(",")); 252 else 253 return new ArrayList (); 254 } 255 256 261 262 public boolean getIsIPAllowed(HttpServletRequest request) 263 { 264 boolean isIPAllowed = false; 265 266 if(useUpdateSecurity != null && useUpdateSecurity.equals("true")) 268 { 269 String remoteIP = request.getRemoteAddr(); 270 if(remoteIP.equals("127.0.0.1")) 272 { 273 isIPAllowed = true; 274 } 275 else 276 { 277 List allowedAdminIPList = ServerNodeController.getController().getAllowedAdminIPList(); 278 Iterator i = allowedAdminIPList.iterator(); 279 while(i.hasNext()) 280 { 281 String allowedIP = (String )i.next(); 282 if(!allowedIP.trim().equals("")) 283 { 284 int index = allowedIP.indexOf(".*"); 286 if(index > -1) 287 allowedIP = allowedIP.substring(0, index); 288 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 getAllowedAdminIP() 306 { 307 Map args = new HashMap (); 308 args.put("globalKey", "infoglue"); 309 PropertySet ps = PropertySetManager.getInstance("jdbc", args); 310 311 String allowedAdminIP = ps.getString("allowedAdminIP"); 312 313 return allowedAdminIP; 314 } 315 316 public void setAllowedAdminIP(String allowedAdminIP) 317 { 318 Map args = new HashMap (); 319 args.put("globalKey", "infoglue"); 320 PropertySet ps = PropertySetManager.getInstance("jdbc", args); 321 322 ps.setString("allowedAdminIP", allowedAdminIP); 323 } 324 325 329 330 public BaseEntityVO getNewVO() 331 { 332 return new ServerNodeVO(); 333 } 334 335 } 336 337 | Popular Tags |