001: /**
002: * $Id: ProfileMapManager.java,v 1.7 2006/06/16 10:58:25 kk155903 Exp $
003: * Copyright 2003 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wsrp.producer;
014:
015: import java.util.Set;
016: import java.util.Map;
017: import java.util.HashSet;
018: import java.util.HashMap;
019: import java.util.Iterator;
020: import java.util.Collection;
021: import java.util.Collections;
022:
023: import java.io.ByteArrayInputStream;
024: import java.io.ByteArrayOutputStream;
025: import java.io.InputStream;
026: import java.io.UnsupportedEncodingException;
027:
028: import javax.xml.bind.JAXBContext;
029: import javax.xml.bind.JAXBException;
030: import javax.xml.bind.Marshaller;
031: import javax.xml.bind.Unmarshaller;
032:
033: import com.iplanet.sso.SSOToken;
034:
035: import com.sun.portal.wsrp.producer.ISConnection;
036: import com.sun.portal.wsrp.producer.ProducerRegistryManager;
037: import com.sun.portal.wsrp.producer.ProducerDN;
038:
039: import com.sun.portal.wsrp.common.jaxb.producer.ProfileMap;
040: import com.sun.portal.wsrp.common.jaxb.producer.AttributeMappingType;
041: import com.sun.portal.wsrp.common.jaxb.producer.ObjectFactory;
042:
043: public class ProfileMapManager implements ISConstants {
044: private static final int PORTLET = 0;
045: private static final int WSRP = 1;
046:
047: private static ProfileMap globalProfileMap = null;
048:
049: private ProfileMap profileMap = null;
050: private ISConnection isConnection = null;
051: private SSOToken token = null;
052: private String organization = null;
053: private String instance = null;
054: private ProducerJAXB producerJAXB = null;
055: private String portalId = null;
056:
057: private ProfileMapManager() {
058: }
059:
060: public ProfileMapManager(SSOToken token, String producerKey)
061: throws ProducerException {
062: ProducerRegistryManager prm = ProducerRegistryManager
063: .getRegistryManager();
064: this .organization = prm.getOrganization(producerKey);
065: this .instance = prm.getInstance(producerKey);
066:
067: this .token = token;
068: producerJAXB = new ProducerJAXB();
069: }
070:
071: public ProfileMapManager(SSOToken token, String producerKey,
072: String portalId) throws ProducerException {
073: this .portalId = portalId;
074: ProducerRegistryManager prm = ProducerRegistryManager
075: .getRegistryManager(portalId);
076: this .organization = prm.getOrganization(producerKey);
077: this .instance = prm.getInstance(producerKey);
078:
079: this .token = token;
080: producerJAXB = new ProducerJAXB();
081: }
082:
083: private ISConnection getISConnection() throws ProducerException {
084: if (isConnection == null) {
085: if (portalId == null) {
086: isConnection = new ISConnection(token);
087: } else {
088: isConnection = new ISConnection(token, portalId);
089: }
090: }
091:
092: return isConnection;
093: }
094:
095: public Map getPortletMap() throws ProducerException {
096: return getMap(PORTLET);
097: }
098:
099: public Map getWSRPMap() throws ProducerException {
100: return getMap(WSRP);
101: }
102:
103: public Map getMap(int byKey) throws ProducerException {
104: Map m = new HashMap();
105:
106: ProfileMap pm = getProfileMap();
107: for (Iterator i = pm.getAttributeMapping().iterator(); i
108: .hasNext();) {
109: AttributeMappingType am = (AttributeMappingType) i.next();
110: String portlet = am.getPortlet();
111: String wsrp = am.getWSRP();
112:
113: if (byKey == PORTLET) {
114: m.put(portlet, wsrp);
115: } else if (byKey == WSRP) {
116: m.put(wsrp, portlet);
117: } else {
118: throw new ProducerException("unknown byKey=" + byKey);
119: }
120: }
121:
122: return m;
123: }
124:
125: public String getPortletMapping(String wsrpName)
126: throws ProducerException {
127: Map m = getWSRPMap();
128: return (String) m.get(wsrpName);
129: }
130:
131: public String getWSRPMapping(String portletName)
132: throws ProducerException {
133: Map m = getPortletMap();
134: return (String) m.get(portletName);
135: }
136:
137: public void addMapping(String portletName, String wsrpName)
138: throws ProducerException {
139: addMappings(Collections.singletonMap(portletName, wsrpName));
140: }
141:
142: public void addMappings(Map mappings) throws ProducerException {
143: ProfileMap pm = getProfileMap();
144:
145: for (Iterator i = mappings.keySet().iterator(); i.hasNext();) {
146: String portletName = (String) i.next();
147: String wsrpName = (String) mappings.get(portletName);
148:
149: AttributeMappingType am = null;
150: try {
151: am = producerJAXB.getObjectFactory()
152: .createAttributeMappingType();
153: } catch (JAXBException je) {
154: throw new ProducerException(je);
155: }
156:
157: am.setPortlet(portletName);
158: am.setWSRP(wsrpName);
159:
160: pm.getAttributeMapping().add(am);
161: }
162:
163: store();
164: }
165:
166: public void removeMappingByPortlet(String portletName)
167: throws ProducerException {
168: removeMappingsByPortlet(Collections.singletonList(portletName));
169: }
170:
171: public void removeMappingsByPortlet(Collection portletNames)
172: throws ProducerException {
173: removeMappings(portletNames, PORTLET);
174: }
175:
176: public void removeMappingByWSRP(String wsrpName)
177: throws ProducerException {
178: removeMappingsByWSRP(Collections.singletonList(wsrpName));
179: }
180:
181: public void removeMappingsByWSRP(Collection wsrpNames)
182: throws ProducerException {
183: removeMappings(wsrpNames, WSRP);
184: }
185:
186: public void removeMappings(Collection names, int byKey)
187: throws ProducerException {
188: ProfileMap pm = getProfileMap();
189: for (Iterator i = pm.getAttributeMapping().iterator(); i
190: .hasNext();) {
191: AttributeMappingType am = (AttributeMappingType) i.next();
192:
193: if (byKey == PORTLET) {
194: String portlet = am.getPortlet();
195: if (names.contains(portlet)) {
196: pm.getAttributeMapping().remove(am);
197: }
198: } else if (byKey == WSRP) {
199: String wsrp = am.getWSRP();
200: if (names.contains(wsrp)) {
201: pm.getAttributeMapping().remove(am);
202: }
203: }
204: }
205: }
206:
207: private ProfileMap getProfileMap() throws ProducerException {
208: if (profileMap == null) {
209: String dn = ProducerDN.getProducerDN(organization,
210: instance, portalId);
211: String xml = getISConnection().getStringAttribute(dn,
212: ATTR_PROFILE_MAP);
213:
214: //ISConnection.debug.error("ProfileMapManager.getProfileMap(): xml=" + xml);
215:
216: if (xml == null || xml.length() == 0) {
217: //
218: // return the global profile map
219: // the global profile map may be empty
220: //
221: profileMap = getGlobalProfileMap();
222: } else {
223: InputStream is = null;
224: try {
225: is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
226: } catch (UnsupportedEncodingException uee) {
227: throw new ProducerException(uee);
228: }
229:
230: try {
231: profileMap = (ProfileMap) producerJAXB
232: .getUnmarshaller().unmarshal(is);
233: } catch (JAXBException je) {
234: throw new ProducerException(
235: "could not unmarshal profile map", je);
236: }
237: }
238: }
239:
240: return profileMap;
241: }
242:
243: private ProfileMap getGlobalProfileMap() throws ProducerException {
244: synchronized (this .getClass()) {
245: if (globalProfileMap == null) {
246: String xml = getISConnection()
247: .getGlobalStringAttribute(
248: ATTR_STANDARD_PROFILE_MAP);
249:
250: //ISConnection.debug.error("ProfileMapManager.getGlobalProfileMap(): got xml=" + xml);
251:
252: if (xml != null && xml.length() != 0) {
253: InputStream is = null;
254: try {
255: is = new ByteArrayInputStream(xml
256: .getBytes("UTF-8"));
257: } catch (UnsupportedEncodingException uee) {
258: throw new ProducerException(uee);
259: }
260:
261: try {
262: globalProfileMap = (ProfileMap) producerJAXB
263: .getUnmarshaller().unmarshal(is);
264: } catch (JAXBException je) {
265: throw new ProducerException(
266: "could not unmarshal profile map", je);
267: }
268: } else {
269: try {
270: globalProfileMap = producerJAXB
271: .getObjectFactory().createProfileMap();
272: } catch (JAXBException je) {
273: throw new ProducerException(
274: "could not create new profile map", je);
275: }
276: }
277: }
278: }
279:
280: return globalProfileMap;
281: }
282:
283: private void store() throws ProducerException {
284: String s = getXML();
285: String dn = ProducerDN.getProducerDN(organization, instance,
286: portalId);
287: getISConnection().setStringAttribute(dn, ATTR_PROFILE_MAP, s);
288: }
289:
290: public String getXML() throws ProducerException {
291: ByteArrayOutputStream os = new ByteArrayOutputStream();
292:
293: try {
294: producerJAXB.getMarshaller().marshal(getProfileMap(), os);
295: } catch (JAXBException je) {
296: throw new ProducerException(
297: "could not marshal profile map", je);
298: }
299:
300: String s = null;
301: try {
302: s = os.toString("UTF-8");
303: } catch (UnsupportedEncodingException uee) {
304: throw new ProducerException(
305: "could not encode marshalled string s=" + s, uee);
306: }
307:
308: return s;
309: }
310: }
|