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.jcr;
019:
020: import java.util.ArrayList;
021: import java.util.Arrays;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: import javax.jcr.AccessDeniedException;
028: import javax.jcr.LoginException;
029: import javax.jcr.NamespaceException;
030: import javax.jcr.NamespaceRegistry;
031: import javax.jcr.NoSuchWorkspaceException;
032: import javax.jcr.PropertyType;
033: import javax.jcr.RepositoryException;
034: import javax.jcr.UnsupportedRepositoryOperationException;
035:
036: import org.apache.avalon.framework.configuration.Configuration;
037: import org.apache.avalon.framework.configuration.ConfigurationException;
038: import org.apache.cocoon.components.ContextHelper;
039: import org.apache.cocoon.environment.ObjectModelHelper;
040: import org.apache.cocoon.environment.Request;
041: import org.apache.cocoon.environment.Session;
042: import org.apache.jackrabbit.core.nodetype.InvalidNodeTypeDefException;
043: import org.apache.jackrabbit.core.nodetype.NodeTypeDef;
044: import org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl;
045: import org.apache.jackrabbit.core.nodetype.NodeTypeRegistry;
046: import org.apache.jackrabbit.core.nodetype.PropDef;
047: import org.apache.jackrabbit.core.nodetype.PropDefImpl;
048: import org.apache.jackrabbit.name.QName;
049: import org.apache.lenya.cms.metadata.dublincore.DublinCore;
050:
051: /**
052: * Lenya-specific repository implementation.
053: */
054: public class LenyaRepository extends
055: org.apache.cocoon.jcr.JackrabbitRepository {
056:
057: protected static final String CONTENT_NODE = "contentNode";
058:
059: protected static final String SESSION_ATTRIBUTE = javax.jcr.Session.class
060: .getName();
061:
062: private Map namespaces = new HashMap();
063:
064: /**
065: * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
066: */
067: public void configure(Configuration config)
068: throws ConfigurationException {
069: super .configure(config);
070:
071: Configuration[] namespaceConfigs = config
072: .getChildren("namespace");
073: for (int i = 0; i < namespaceConfigs.length; i++) {
074: String prefix = namespaceConfigs[i].getAttribute("prefix");
075: String uri = namespaceConfigs[i].getAttribute("uri");
076: this .namespaces.put(prefix, uri);
077: }
078: }
079:
080: /**
081: * @see javax.jcr.Repository#login()
082: */
083: public javax.jcr.Session login() throws LoginException,
084: NoSuchWorkspaceException, RepositoryException {
085:
086: javax.jcr.Session jcrSession = null;
087:
088: Map objectModel = ContextHelper.getObjectModel(this .context);
089: Request request = ObjectModelHelper.getRequest(objectModel);
090: Session session = request.getSession(false);
091: if (session != null) {
092: jcrSession = (javax.jcr.Session) session
093: .getAttribute(SESSION_ATTRIBUTE);
094: if (jcrSession == null) {
095: jcrSession = super .login();
096: session.setAttribute(SESSION_ATTRIBUTE, jcrSession);
097:
098: registerNamespaces(jcrSession);
099: try {
100: registerNodeTypes(jcrSession);
101: } catch (InvalidNodeTypeDefException e) {
102: throw new RepositoryException(e);
103: }
104: }
105: }
106:
107: return jcrSession;
108: }
109:
110: /**
111: * Registers the Lenya-specific namespaces at the JCR workspace.
112: * @param jcrSession The JCR session.
113: * @throws RepositoryException if an error occurs.
114: * @throws NamespaceException if an error occurs.
115: * @throws UnsupportedRepositoryOperationException if an error occurs.
116: * @throws AccessDeniedException if an error occurs.
117: */
118: protected void registerNamespaces(javax.jcr.Session jcrSession)
119: throws RepositoryException, NamespaceException,
120: UnsupportedRepositoryOperationException,
121: AccessDeniedException {
122: for (Iterator i = this .namespaces.keySet().iterator(); i
123: .hasNext();) {
124: String prefix = (String) i.next();
125: String uri = (String) this .namespaces.get(prefix);
126: NamespaceRegistry registry = jcrSession.getWorkspace()
127: .getNamespaceRegistry();
128: if (!Arrays.asList(registry.getPrefixes()).contains(prefix)) {
129: registry.registerNamespace(prefix, uri);
130: }
131: }
132: }
133:
134: protected void registerNodeTypes(javax.jcr.Session jcrSession)
135: throws RepositoryException, InvalidNodeTypeDefException {
136:
137: NodeTypeManagerImpl nodeTypeManager = (NodeTypeManagerImpl) jcrSession
138: .getWorkspace().getNodeTypeManager();
139: NodeTypeRegistry registry = nodeTypeManager
140: .getNodeTypeRegistry();
141: /*
142: if (!registry.isRegistered(new QName(LenyaMetaData.NAMESPACE, CONTENT_NODE))) {
143:
144: List propDefs = new ArrayList();
145: Map key2namespace = new HashMap();
146:
147: String[] lenyaKeys = LenyaMetaData.ELEMENTS;
148: for (int i = 0; i < lenyaKeys.length; i++) {
149: key2namespace.put(lenyaKeys[i], LenyaMetaData.NAMESPACE);
150: }
151:
152: List dcKeyList = DublinCoreImpl.getAttributeNames();
153: String[] dcKeys = (String[]) dcKeyList.toArray(new String[dcKeyList.size()]);
154: for (int i = 0; i < dcKeys.length; i++) {
155: key2namespace.put(dcKeys[i], DublinCore.DC_NAMESPACE);
156: }
157:
158: NodeTypeDef def = new NodeTypeDef();
159: def.setMixin(true);
160: def.setName(new QName(LenyaMetaData.NAMESPACE, CONTENT_NODE));
161:
162: for (Iterator i = key2namespace.keySet().iterator(); i.hasNext();) {
163: String key = (String) i.next();
164: String namespace = (String) key2namespace.get(key);
165: PropDefImpl propDef = new PropDefImpl();
166: propDef.setDeclaringNodeType(def.getName());
167: propDef.setName(new QName(namespace, key));
168: propDef.setMandatory(false);
169: propDef.setRequiredType(PropertyType.STRING);
170: propDef.setMultiple(true);
171: propDefs.add(propDef);
172: }
173:
174: def.setPropertyDefs((PropDef[]) propDefs.toArray(new PropDef[propDefs.size()]));
175:
176: registry.registerNodeType(def);
177: }
178: */
179: }
180:
181: }
|