001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * any nuclear facility.
035: */
036:
037: package com.sun.portal.community.template.impl;
038:
039: import java.io.InputStream;
040: import java.io.ByteArrayInputStream;
041: import java.io.InputStreamReader;
042: import java.io.BufferedReader;
043: import java.io.IOException;
044:
045: import java.util.Iterator;
046: import java.util.Set;
047: import java.util.Map;
048: import java.util.HashMap;
049: import java.util.Locale;
050:
051: import com.sun.portal.community.mc.CMCRolePrincipal;
052:
053: import com.sun.portal.community.template.Template;
054: import com.sun.portal.community.template.TemplateException;
055:
056: public class CommunityTemplateImpl implements Template {
057:
058: private String _id;
059: private Map _templates;
060:
061: CommunityTemplateImpl(String id, Map templates) {
062:
063: _id = id;
064: _templates = templates;
065: }
066:
067: public String getId() {
068:
069: return _id;
070: }
071:
072: public Set getRoleTemplateNames() throws TemplateException {
073:
074: return _templates.keySet();
075: }
076:
077: public byte[] getRoleTemplate(String role) throws TemplateException {
078:
079: CMCRolePrincipal rolePrincipal = CMCRolePrincipal.valueOf(role);
080: if (rolePrincipal == null) {
081: throw new TemplateException(
082: "CommunityTemplateImpl.getRoleTemplate(): Invalid role="
083: + role);
084: }
085: return (byte[]) _templates.get(role);
086: }
087:
088: public Map getRoleTemplates() {
089:
090: return _templates;
091: }
092:
093: public String toString() {
094:
095: StringBuffer buf = new StringBuffer();
096:
097: String line = null;
098: try {
099: for (Iterator i = _templates.keySet().iterator(); i
100: .hasNext();) {
101: String role = (String) i.next();
102: buf.append("\nRole[\"" + role + "\"] = ");
103: ByteArrayInputStream is = new ByteArrayInputStream(
104: (byte[]) _templates.get(role));
105: InputStreamReader isr = new InputStreamReader(is);
106: BufferedReader br = new BufferedReader(isr);
107: while ((line = br.readLine()) != null) {
108: buf.append(line + "\n");
109: }
110: }
111: } catch (IOException ioe) {
112: // handle gracefully
113: return "<ERROR>";
114: }
115:
116: return buf.toString();
117: }
118: }
|