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 java.io.InputStream;
021: import java.util.Iterator;
022: import java.util.Vector;
023:
024: import org.apache.avalon.framework.container.ContainerUtil;
025: import org.apache.avalon.framework.logger.AbstractLogEnabled;
026: import org.apache.avalon.framework.logger.Logger;
027: import org.apache.avalon.framework.service.ServiceManager;
028: import org.apache.excalibur.source.Source;
029: import org.apache.excalibur.source.SourceResolver;
030: import org.apache.lenya.cms.cocoon.source.SourceUtil;
031: import org.apache.lenya.cms.metadata.MetaData;
032: import org.apache.lenya.cms.metadata.MetaDataException;
033: import org.apache.lenya.cms.rc.CheckInEntry;
034: import org.apache.lenya.cms.rc.RCML;
035: import org.apache.lenya.cms.rc.RCMLEntry;
036:
037: /**
038: * Revision implementation.
039: */
040: public class SourceNodeRevision extends AbstractLogEnabled implements
041: Revision {
042:
043: private SourceNode node;
044: private int number;
045: private ServiceManager manager;
046: private long time = -1;
047: private String userId;
048:
049: /**
050: * @param node The node.
051: * @param number The revision number.
052: * @param manager The service manager.
053: * @param logger The logger.
054: */
055: public SourceNodeRevision(SourceNode node, int number,
056: ServiceManager manager, Logger logger) {
057: this .node = node;
058: this .number = number;
059: this .manager = manager;
060: ContainerUtil.enableLogging(this , logger);
061: }
062:
063: public long getTime() {
064: initialize();
065: return this .time;
066: }
067:
068: protected void initialize() {
069: try {
070: if (this .time == -1) {
071: SourceNodeRCML rcml = (SourceNodeRCML) this .node
072: .getRcml();
073: Vector entries = rcml.getEntries();
074: for (Iterator i = entries.iterator(); i.hasNext();) {
075: RCMLEntry entry = (RCMLEntry) i.next();
076: if (entry.getType() == RCML.ci
077: && ((CheckInEntry) entry).getVersion() == this .number) {
078: this .time = entry.getTime();
079: this .userId = entry.getIdentity();
080: }
081: }
082: }
083: if (this .time == -1) {
084: throw new RuntimeException("No entry found for ["
085: + this .node.getSourceURI() + "], revision ["
086: + this .number + "]");
087: }
088: } catch (Exception e) {
089: throw new RuntimeException(e);
090: }
091: }
092:
093: public InputStream getInputStream() {
094: Source source = null;
095: SourceResolver resolver = null;
096: try {
097: String sourceUri = getSourceURI();
098: resolver = (SourceResolver) this .manager
099: .lookup(SourceResolver.ROLE);
100: source = resolver.resolveURI(sourceUri);
101: if (source.exists()) {
102: return source.getInputStream();
103: } else {
104: throw new RuntimeException(
105: "No check-in entry found for ["
106: + this .node.getSourceURI()
107: + "], revision [" + this .number + "]");
108: }
109: } catch (RuntimeException e) {
110: throw e;
111: } catch (Exception e) {
112: throw new RuntimeException(e);
113: } finally {
114: if (resolver != null) {
115: if (source != null) {
116: resolver.release(source);
117: }
118: this .manager.release(resolver);
119: }
120: }
121: }
122:
123: public String getSourceURI() {
124: SourceNodeRCML rcml = (SourceNodeRCML) this .node.getRcml();
125: String sourceUri = rcml.getBackupSourceUri(this .node
126: .getContentSource(), getTime());
127: return sourceUri;
128: }
129:
130: public int getNumber() {
131: return this .number;
132: }
133:
134: public MetaData getMetaData(String namespaceUri)
135: throws MetaDataException {
136: return getMetaDataHandler().getMetaData(namespaceUri);
137: }
138:
139: private SourceNodeMetaDataHandler metaDataHandler = null;
140:
141: protected SourceNodeMetaDataHandler getMetaDataHandler() {
142: if (this .metaDataHandler == null) {
143: this .metaDataHandler = new SourceNodeMetaDataHandler(
144: this .manager, getMetaSourceUri());
145: }
146: return this .metaDataHandler;
147: }
148:
149: protected String getMetaSourceUri() {
150: String realSourceUri = SourceWrapper.computeRealSourceUri(
151: this .manager, this .node.getSession(), this .node
152: .getSourceURI(), getLogger());
153: return realSourceUri + ".meta." + getTime() + ".bak";
154: }
155:
156: public String[] getMetaDataNamespaceUris() throws MetaDataException {
157: return getMetaDataHandler().getMetaDataNamespaceUris();
158: }
159:
160: public boolean exists() throws RepositoryException {
161: try {
162: return SourceUtil.exists(getSourceURI(), this .manager);
163: } catch (Exception e) {
164: throw new RepositoryException(e);
165: }
166: }
167:
168: public long getContentLength() throws RepositoryException {
169: try {
170: return SourceUtil.getContentLength(getSourceURI(),
171: this .manager);
172: } catch (Exception e) {
173: throw new RepositoryException(e);
174: }
175: }
176:
177: public long getLastModified() throws RepositoryException {
178: try {
179: return SourceUtil.getLastModified(getSourceURI(),
180: this .manager);
181: } catch (Exception e) {
182: throw new RepositoryException(e);
183: }
184: }
185:
186: public String getMimeType() throws RepositoryException {
187: try {
188: return SourceUtil.getMimeType(getSourceURI(), this .manager);
189: } catch (Exception e) {
190: throw new RepositoryException(e);
191: }
192: }
193:
194: public String getUserId() {
195: initialize();
196: return this.userId;
197: }
198:
199: }
|