1 7 package org.exoplatform.services.wsrp.producer.impl; 8 9 import java.rmi.RemoteException ; 10 import java.util.*; 11 import org.apache.commons.lang.StringUtils; 12 import org.apache.commons.logging.Log; 13 import org.exoplatform.Constants; 14 import org.exoplatform.commons.utils.IdentifierUtil; 15 import org.exoplatform.services.log.LogService; 16 import org.exoplatform.services.wsrp.exceptions.Exception2Fault; 17 import org.exoplatform.services.wsrp.exceptions.Faults; 18 import org.exoplatform.services.wsrp.exceptions.WSRPException; 19 import org.exoplatform.services.wsrp.producer.PersistentStateManager; 20 import org.exoplatform.services.wsrp.producer.PortletContainerProxy; 21 import org.exoplatform.services.wsrp.producer.PortletManagementOperationsInterface; 22 import org.exoplatform.services.wsrp.type.*; 23 24 28 public class PortletManagementOperationsInterfaceImpl implements PortletManagementOperationsInterface { 29 30 private PortletContainerProxy container; 31 private Log log; 32 private PersistentStateManager stateManager; 33 34 public PortletManagementOperationsInterfaceImpl(PersistentStateManager stateManager, 35 PortletContainerProxy container, 36 LogService logService) { 37 this.container = container; 38 this.stateManager = stateManager; 39 this.log = logService.getLog("org.exoplatform.services.wsrp"); 40 } 41 42 public PortletContext clonePortlet(RegistrationContext registrationContext, 43 PortletContext portletContext, 44 UserContext userContext) 45 throws RemoteException { 46 String registrationHandle = registrationContext.getRegistrationHandle(); 48 log.debug("Clone a portlet for the registered consumer : " + registrationHandle); 49 org.exoplatform.services.wsrp.producer.impl.utils.Utils.testRegistration(registrationContext, stateManager); 50 51 String portletHandle = portletContext.getPortletHandle(); 52 String newPortletHandle = null; 53 try { 54 if (stateManager.isConsumerConfiguredPortlet(portletHandle, registrationContext)) { 55 log.debug("Clone a Consumer Configured Portlet with handle : " + portletHandle); 56 PropertyList list = getPortletProperties(registrationContext, portletContext, userContext, null); 57 newPortletHandle = createNewPortletHandle(portletHandle); 58 stateManager.addConsumerConfiguredPortletHandle(newPortletHandle, registrationContext); 59 portletContext.setPortletHandle(newPortletHandle); 60 setPortletProperties(registrationContext, portletContext, userContext, list); 61 } else { 62 log.debug("Clone a Producer Offered Portlet with handle : " + portletHandle); 63 if (!container.isPortletOffered(portletHandle)) { 64 log.debug("The latter handle is not offered by the Producer"); 65 Exception2Fault.handleException(new WSRPException(Faults.INVALID_HANDLE_FAULT)); 66 } 67 newPortletHandle = createNewPortletHandle(portletHandle); 68 stateManager.addConsumerConfiguredPortletHandle(newPortletHandle, registrationContext); 69 } 70 } catch (WSRPException e) { 71 log.error("Can not clone portlet", e); 72 Exception2Fault.handleException(e); 73 } 74 log.debug("New portlet handle : " + newPortletHandle); 75 PortletContext clonedPortletContext = new PortletContext(); 76 clonedPortletContext.setPortletHandle(newPortletHandle); 77 return clonedPortletContext; 78 } 79 80 public DestroyPortletsResponse destroyPortlets(RegistrationContext registrationContext, 81 String [] portletHandles) 82 throws RemoteException { 83 String registrationHandle = registrationContext.getRegistrationHandle(); 85 log.debug("Destroy portlet for registration handle " + registrationHandle); 86 org.exoplatform.services.wsrp.producer.impl.utils.Utils.testRegistration(registrationContext, stateManager); 87 88 Collection fails = new ArrayList(); 89 for (int i = 0; i < portletHandles.length; i++) { 90 String portletHandle = portletHandles[i]; 91 try { 92 if (stateManager.isConsumerConfiguredPortlet(portletHandle, registrationContext)) { 93 log.debug("Destroy a consumer configured portlet : " + portletHandle); 94 stateManager.removeConsumerConfiguredPortletHandle(portletHandle, registrationContext); 95 } else { 96 log.debug("Can't destroy a portlet that did not exist : " + portletHandle); 97 DestroyFailed destroyFailed = new DestroyFailed(); 98 destroyFailed.setPortletHandle(portletHandle); 99 destroyFailed.setReason("Can't destroy a portlet that did not exist"); 100 fails.add(destroyFailed); 101 } 102 } catch (WSRPException e) { 103 Exception2Fault.handleException(e); 104 } 105 } 106 DestroyPortletsResponse response = new DestroyPortletsResponse(); 107 DestroyFailed[] array = (DestroyFailed[]) fails.toArray(new DestroyFailed[fails.size()]); 108 if(array != null) 109 response.setDestroyFailed(array); 110 return response; 111 } 112 113 public PortletDescriptionResponse getPortletDescription(RegistrationContext registrationContext, 114 PortletContext portletContext, 115 UserContext userContext, 116 String [] desiredLocales) 117 throws RemoteException { 118 String registrationHandle = registrationContext.getRegistrationHandle(); 120 log.debug("Get portlet description for registration handle " + registrationHandle); 121 org.exoplatform.services.wsrp.producer.impl.utils.Utils.testRegistration(registrationContext, stateManager); 122 123 String portletHandle = portletContext.getPortletHandle(); 124 125 try { 126 if (!stateManager.isConsumerConfiguredPortlet(portletHandle, registrationContext)) { 127 log.debug("This portlet handle " + portletHandle + " is not valid in the scope of that registration "); 128 Exception2Fault.handleException(new WSRPException(Faults.ACCESS_DENIED_FAULT)); 129 } 130 } catch (WSRPException e) { 131 Exception2Fault.handleException(e); 132 } 133 134 PortletDescription pD = container.getPortletDesciption(portletHandle, desiredLocales); 135 ResourceList resourceList = container.getResourceList(desiredLocales); 136 137 PortletDescriptionResponse response = new PortletDescriptionResponse(); 138 response.setPortletDescription(pD); 139 response.setResourceList(resourceList); 140 141 return response; 142 } 143 144 public PortletContext setPortletProperties(RegistrationContext registrationContext, 145 PortletContext portletContext, 146 UserContext userContext, 147 PropertyList propertyList) 148 throws RemoteException { 149 String registrationHandle = registrationContext.getRegistrationHandle(); 151 log.debug("Set portlet properties for registration handle " + registrationHandle); 152 org.exoplatform.services.wsrp.producer.impl.utils.Utils.testRegistration(registrationContext, stateManager); 153 String portletHandle = portletContext.getPortletHandle(); 154 155 try { 156 if (!stateManager.isConsumerConfiguredPortlet(portletHandle, registrationContext)) { 157 log.debug("This portlet handle " + portletHandle + " is not valid in the scope of that registration "); 158 Exception2Fault.handleException(new WSRPException(Faults.ACCESS_DENIED_FAULT)); 159 } 160 } catch (WSRPException e) { 161 Exception2Fault.handleException(e); 162 } 163 164 String userID = userContext.getUserContextKey(); 165 166 try { 167 container.setPortletProperties(portletHandle, userID, propertyList); 168 } catch (WSRPException e) { 169 Exception2Fault.handleException(e); 170 } 171 return portletContext; 172 } 173 174 public PropertyList getPortletProperties(RegistrationContext registrationContext, 175 PortletContext portletContext, 176 UserContext userContext, 177 String [] names) 178 throws RemoteException { 179 String registrationHandle = registrationContext.getRegistrationHandle(); 181 log.debug("get portlet properties for registration handle " + registrationHandle); 182 org.exoplatform.services.wsrp.producer.impl.utils.Utils.testRegistration(registrationContext, stateManager); 183 String portletHandle = portletContext.getPortletHandle(); 184 try { 185 if (!stateManager.isConsumerConfiguredPortlet(portletHandle, registrationContext)) { 186 log.debug("This portlet handle " + portletHandle + " is not valid in the scope of that registration "); 187 Exception2Fault.handleException(new WSRPException(Faults.ACCESS_DENIED_FAULT)); 188 } 189 } catch (WSRPException e) { 190 Exception2Fault.handleException(e); 191 } 192 String userID = userContext.getUserContextKey(); 193 Map properties = null; 194 try { 195 properties = container.getPortletProperties(portletHandle, userID); 196 } catch (WSRPException e) { 197 Exception2Fault.handleException(e); 198 } 199 Collection properties2return = new ArrayList(); 200 Set keys = properties.keySet(); 201 for (Iterator iterator = keys.iterator(); iterator.hasNext();) { 202 String key = (String ) iterator.next(); 203 if (names == null || arrayContainsKey(names, key)) { 204 String [] values = (String []) properties.get(key); 205 Property prop = new Property(); 206 prop.setName(key); 207 prop.setStringValue(values[0]); 208 properties2return.add(prop); 209 } 210 } 211 PropertyList list = new PropertyList(); 212 list.setProperties((Property[]) properties2return.toArray(new Property[properties2return.size()])); 213 list.setResetProperties(null); 214 return list; 215 } 216 217 public PortletPropertyDescriptionResponse getPortletPropertyDescription(RegistrationContext registrationContext, 218 PortletContext portletContext, 219 UserContext userContext, 220 String [] desiredLocales) 221 throws RemoteException { 222 return new PortletPropertyDescriptionResponse(); 223 } 224 225 226 private boolean arrayContainsKey(String [] array, String key) { 227 for (int i = 0; i < array.length; i++) { 228 String s = array[i]; 229 if (s.equals(key)) { 230 return true; 231 } 232 } 233 return false; 234 } 235 236 private String createNewPortletHandle(String portletHandle) { 237 String [] keys = StringUtils.split(portletHandle, Constants.PORTLET_HANDLE_ENCODER); 238 return keys[0] + Constants.PORTLET_HANDLE_ENCODER + keys[1] + Constants.PORTLET_HANDLE_ENCODER + 239 IdentifierUtil.generateUUID(portletHandle); 240 } 241 242 } 243 | Popular Tags |