1 16 package org.apache.pluto.portlet.admin.model; 17 18 import java.io.FileReader ; 19 import java.io.FileWriter ; 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 24 import org.apache.pluto.portalImpl.om.entity.impl.PortletApplicationEntityImpl; 25 import org.apache.pluto.portalImpl.om.entity.impl.PortletApplicationEntityListImpl; 26 import org.apache.pluto.portalImpl.om.entity.impl.PortletEntityImpl; 27 import org.apache.pluto.portlet.admin.BaseAdminObject; 28 import org.apache.pluto.portlet.admin.PlutoAdminException; 29 import org.apache.pluto.portlet.admin.util.PlutoAdminContext; 30 import org.apache.pluto.util.StringUtils; 31 import org.exolab.castor.mapping.Mapping; 32 import org.exolab.castor.xml.Marshaller; 33 import org.exolab.castor.xml.Unmarshaller; 34 35 44 public class PortletEntityRegistryXao extends BaseAdminObject { 45 46 47 public final static String CONFIG_FILE = "WEB-INF/data/portletentityregistry.xml"; 49 public final static String DEFAULT_MAPPING = "WEB-INF/data/xml/portletentitymapping.xml"; 50 private final static String CLASS_NAME = "PortletEntityRegistryXao"; 51 private Collection castorApplications = new ArrayList (); 53 54 private Mapping mapping = null; 56 57 60 public PortletEntityRegistryXao() throws Exception { 61 super(CLASS_NAME); 62 init(); 63 } 64 public void init () throws Exception 65 { 66 final String METHOD_NAME = "init()"; 67 68 String _mapping = PlutoAdminContext.getInstance().getPlutoHome() + "/" + DEFAULT_MAPPING; 69 70 this.mapping = new Mapping(); 71 try 72 { 73 this.mapping.loadMapping(_mapping); 74 } 75 catch (Exception e) 76 { 77 logError(METHOD_NAME, "Failed to load mapping file "+_mapping,e); 78 throw e; 79 } 80 81 } 82 83 84 85 public void save(PortletApplicationEntityListImpl apps) throws Exception 86 { 87 final String METHOD_NAME = "save(AdminPortalImpl)"; 88 String filename = PlutoAdminContext.getInstance().getPlutoHome() + "/" + CONFIG_FILE; 89 logDebug(METHOD_NAME, "Registry file to save: " + filename); 90 91 FileWriter writer = new FileWriter (filename); 92 93 Marshaller marshaller = new Marshaller(writer); 94 95 marshaller.setMapping(this.mapping); 96 97 marshaller.marshal(apps); 98 } 99 100 public PortletApplicationEntityListImpl load() throws Exception 101 { 102 final String METHOD_NAME = "load()"; 103 104 String filename = PlutoAdminContext.getInstance().getPlutoHome() + "/" + CONFIG_FILE; 105 logDebug(METHOD_NAME, "Registry file to load: " + filename); 106 107 Unmarshaller unmarshaller = new Unmarshaller(this.mapping); 108 unmarshaller.setMapping(this.mapping); 109 110 PortletApplicationEntityListImpl apps = (PortletApplicationEntityListImpl)unmarshaller.unmarshal(new FileReader (filename)); 111 castorApplications = apps.getCastorApplications(); 112 return apps; 113 } 114 115 public String toString() 116 { 117 return toString(0); 118 } 119 120 public String toString(int indent) 121 { 122 StringBuffer buffer = new StringBuffer (1000); 123 StringUtils.newLine(buffer,indent); 124 buffer.append(getClass().toString()); 125 buffer.append(":"); 126 StringUtils.newLine(buffer,indent); 127 buffer.append("{"); 128 Iterator iterator = castorApplications.iterator(); 129 if (iterator.hasNext()) { 130 StringUtils.newLine(buffer,indent); 131 buffer.append("Portlet Application Entities"); 132 int count = castorApplications.size(); 133 buffer.append("("); 134 buffer.append(count); 135 buffer.append("):"); 136 } 137 while (iterator.hasNext()) { 138 buffer.append(((PortletApplicationEntityImpl)iterator.next()).toString(indent+2)); 139 } 140 StringUtils.newLine(buffer,indent); 141 buffer.append("}"); 142 return buffer.toString(); 143 } 144 145 public Collection getApplications() throws Exception { 146 String METHOD_NAME = "getApplications()"; 147 logMethodStart(METHOD_NAME); 148 Collection elist = null; 149 PortletApplicationEntityListImpl per = load(); 150 logDebug(METHOD_NAME, "PER: " + per); 151 if (per != null) { 152 elist = per.getCastorApplications(); 153 } 154 logMethodEnd(METHOD_NAME, elist); 155 return elist; 156 } 157 158 public PortletApplicationEntityImpl getApplication(String castorId) throws Exception { 159 PortletApplicationEntityImpl app = null; 160 Collection apps = getApplications(); 161 Iterator iter = apps.iterator(); 162 while(iter.hasNext()) { 163 PortletApplicationEntityImpl currApp = (PortletApplicationEntityImpl)iter.next(); 164 if (currApp.getCastorId().equalsIgnoreCase(castorId)) { 165 app = currApp; 166 break; 167 } 168 } 169 return app; 170 } 171 172 public Collection getPortletPreferences(String appId, String portletId) throws Exception { 173 Collection list = null; 174 PortletApplicationEntityImpl app = getApplication(appId); 175 Collection plets = app.getCastorPortlets(); 176 Iterator iter = plets.iterator(); 177 while(iter.hasNext()) { 178 PortletEntityImpl plet = (PortletEntityImpl)iter.next(); 179 if (plet.getCastorId().equalsIgnoreCase(portletId)) { 180 list = plet.getCastorPreferences(); 181 break; 182 } 183 } 184 return list; 185 } 186 187 public boolean applicationExists(String appContext) { 188 final String METHOD_NAME = "applicationExists(appContext)"; 189 boolean exists = false; 190 Collection apps = null; 191 try { 192 apps = getApplications(); 193 } catch (Exception e) { 194 logError(METHOD_NAME, e); 195 throw new PlutoAdminException(e); 196 } 197 Iterator iter = apps.iterator(); 198 while (iter.hasNext()) { 199 PortletApplicationEntityImpl app = (PortletApplicationEntityImpl) iter.next(); 200 if (app.getDefinitionId().equalsIgnoreCase(appContext)) { 201 exists = true; 202 break; 203 } 204 } 205 return exists; 206 } 207 } 208 | Popular Tags |