001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.repository;
019:
020: import org.apache.avalon.framework.service.ServiceManager;
021: import org.apache.cocoon.environment.Request;
022: import org.apache.lenya.ac.Identity;
023:
024: /**
025: * Repository utility class.
026: */
027: public class RepositoryUtil {
028:
029: /**
030: * Returns the session attached to the request or creates a session.
031: * @param manager The service manager.
032: * @param request The request.
033: * @return A session.
034: * @throws RepositoryException if an error occurs.
035: */
036: public static Session getSession(ServiceManager manager,
037: Request request) throws RepositoryException {
038: Session session = (Session) request.getAttribute(Session.class
039: .getName());
040: if (session == null) {
041: Identity identity = getIdentity(request);
042: // attach a read-only repository session to the HTTP request
043: session = createSession(manager, identity, false);
044: request.setAttribute(Session.class.getName(), session);
045: } else if (session.getIdentity() == null) {
046: Identity identity = getIdentity(request);
047: if (identity != null) {
048: session.setIdentity(identity);
049: }
050: }
051: return session;
052: }
053:
054: protected static Identity getIdentity(Request request) {
055: org.apache.cocoon.environment.Session cocoonSession = request
056: .getSession();
057: Identity identity = (Identity) cocoonSession
058: .getAttribute(Identity.class.getName());
059: return identity;
060: }
061:
062: /**
063: * Creates a session.
064: * @param manager The service manager.
065: * @param identity The identity.
066: * @param modifiable Determines if the repository items in this session should be modifiable.
067: * @return a session.
068: * @throws RepositoryException if an error occurs.
069: */
070: public static Session createSession(ServiceManager manager,
071: Identity identity, boolean modifiable)
072: throws RepositoryException {
073: RepositoryManager repoMgr = null;
074: Session session;
075: try {
076: repoMgr = (RepositoryManager) manager
077: .lookup(RepositoryManager.ROLE);
078: session = repoMgr.createSession(identity, modifiable);
079: } catch (Exception e) {
080: throw new RepositoryException(e);
081: } finally {
082: manager.release(repoMgr);
083: }
084: return session;
085: }
086:
087: /**
088: * Removes the repository session from the servlet session.
089: * @param manager The service manager.
090: * @param request The current request.
091: */
092: public static void removeSession(ServiceManager manager,
093: Request request) {
094: request.removeAttribute(Session.class.getName());
095: /*
096: org.apache.cocoon.environment.Session session = request.getSession(false);
097: if (session != null) {
098: session.removeAttribute(Session.class.getName());
099: }
100: */
101: }
102:
103: }
|