001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.base;
019:
020: import java.util.Collection;
021: import java.util.Collections;
022: import java.util.HashMap;
023: import java.util.Map;
024: import java.util.logging.Level;
025: import java.util.logging.Logger;
026:
027: import org.xml.sax.helpers.AttributesImpl;
028:
029: import de.finix.contelligent.client.remote.ActionResult;
030: import de.finix.contelligent.client.remote.Actions;
031: import de.finix.contelligent.client.remote.RemoteActionException;
032: import de.finix.contelligent.client.security.ComponentAccess;
033: import de.finix.contelligent.client.security.Principal;
034: import de.zeigermann.xml.simpleImporter.SimpleImportHandler;
035: import de.zeigermann.xml.simpleImporter.SimplePath;
036:
037: public class Context {
038:
039: private static Logger logger = Logger.getLogger(Context.class
040: .getName());
041:
042: private String name;
043:
044: private static Map<String, Context> contexts = new HashMap<String, Context>();
045:
046: // XXX An empty component just to store acl. A component is needed as acl
047: // editor only works on component.
048: private ContelligentComponent aclComponent = null;
049:
050: public static synchronized Context get(String name)
051: throws RemoteActionException {
052: if (contexts.containsKey(name)) {
053: return (Context) contexts.get(name);
054: } else {
055: Context c = new Context(name);
056: contexts.put(name, c);
057: return c;
058: }
059: }
060:
061: private Context(String name) throws RemoteActionException {
062: this .name = name;
063: loadACL();
064: }
065:
066: public String getName() {
067: return name;
068: }
069:
070: public String getShortName() {
071: String mainContextName = ServerInfo.getInstance()
072: .getMainContextName();
073: if (!name.equals(mainContextName)) {
074: String shortContextName = name.substring(mainContextName
075: .length() + 1);
076: if (shortContextName.equals(mainContextName)) {
077: shortContextName = name;
078: }
079: return shortContextName;
080: } else {
081: return name;
082: }
083: }
084:
085: public boolean isRootContext() {
086: // if the context name does not contain '.' we are in the root context
087: return getName().indexOf('.') == -1;
088: }
089:
090: public ContelligentComponent getComponentForEdit() {
091: return aclComponent;
092: }
093:
094: public void loadACL() throws RemoteActionException {
095: ACLImportHandler importHandler = new ACLImportHandler();
096: ActionResult actionResult = Actions.loadContextACL(getName(),
097: importHandler);
098: if (!actionResult.getState().equals("ok")) {
099: actionResult.showErrors();
100: }
101: aclComponent = importHandler.getACLComponent();
102: }
103:
104: public void addAclEntry(ComponentAccess entry) {
105: getComponentForEdit().addAclEntry(entry);
106: }
107:
108: public void removeAclEntry(ComponentAccess entry) {
109: getComponentForEdit().removeAclEntry(entry);
110: }
111:
112: public Collection getACL() {
113: return Collections.unmodifiableCollection(getComponentForEdit()
114: .getACL());
115: }
116:
117: public void setACL(String xml) throws RemoteActionException {
118: ActionResult actionResult = Actions.saveContextACL(getName(),
119: xml);
120: if (!actionResult.getState().equals("ok")) {
121: actionResult.showErrors();
122: }
123: // XXX this is ugly, but the easiest way to do it; anyway editing acls
124: // of context is expected to be done rarely
125: loadACL();
126: }
127:
128: public void addOwner(Principal principal) {
129: getComponentForEdit().addOwner(principal);
130: }
131:
132: public void removeOwner(Principal principal) {
133: getComponentForEdit().removeOwner(principal);
134: }
135:
136: public boolean hasPermission(String permission) {
137: ContelligentComponent aclComponent = getComponentForEdit();
138: // XXX by definition no permissions set means everything for everybody
139: // is allowed
140: if (aclComponent.hasNoPremissions()) {
141: return true;
142: } else {
143: return Session.getInstance().hasPermission(
144: aclComponent.getACL(), permission);
145: }
146: }
147:
148: public boolean canRead() {
149: return hasPermission(ComponentAccess.READ);
150: }
151:
152: public boolean canExecute() {
153: return hasPermission(ComponentAccess.EXECUTE);
154: }
155:
156: public boolean canWrite() {
157: return hasPermission(ComponentAccess.WRITE);
158: }
159:
160: public boolean canDelete() {
161: return hasPermission(ComponentAccess.DELETE);
162: }
163:
164: public Collection getOwners() {
165: return Collections.unmodifiableCollection(getComponentForEdit()
166: .getOwners());
167: }
168:
169: public boolean equals(Object o) {
170: return (o instanceof Context && ((Context) o).getName().equals(
171: getName()));
172: }
173:
174: public int hashCode() {
175: return -1;
176: }
177:
178: public String toString() {
179: return getName();
180: }
181:
182: private final class ACLImportHandler implements SimpleImportHandler {
183:
184: private ContelligentComponent aclComponent;
185:
186: public ContelligentComponent getACLComponent() {
187: return aclComponent;
188: }
189:
190: public void endDocument() {
191: }
192:
193: public void cData(SimplePath path, String cdata) {
194: }
195:
196: public void endElement(SimplePath path, String name) {
197: }
198:
199: public void startDocument() {
200: aclComponent = new ContelligentComponent("dummy", "dummy");
201: // XXX add this just to have something
202: aclComponent.setType(TypeFactory.getInstance()
203: .getDummyType());
204: }
205:
206: public void startElement(SimplePath path, String name,
207: AttributesImpl attrs, String leadingCData) {
208: try {
209:
210: if (path.matches("meta-info/owner")) {
211: Principal currentPrincipal = new Principal(attrs
212: .getValue("principalGroupId"), attrs
213: .getValue("principalId"));
214: aclComponent.addOwner(currentPrincipal);
215:
216: } else if (path.matches("meta-info/access")) {
217: ComponentAccess currentAccess = new ComponentAccess(
218: new Principal(attrs
219: .getValue("principalGroupId"),
220: attrs.getValue("principalId")),
221: attrs.getValue("accessType"), attrs
222: .getValue("mode"), attrs
223: .getValue("parentMode"), Long
224: .parseLong(attrs
225: .getValue("validFrom")),
226: Long.parseLong(attrs.getValue("validTo")),
227: Long.parseLong(attrs.getValue("period")),
228: Long.parseLong(attrs.getValue("duration")));
229: aclComponent.addAclEntry(currentAccess);
230:
231: }
232: } catch (Throwable t) {
233: logger.log(Level.SEVERE,
234: "Caught exception while ACL import", t);
235: }
236: }
237: }
238:
239: }
|