KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > content > AbstractContentStore


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.content;
18
19 import java.util.Calendar JavaDoc;
20 import java.util.GregorianCalendar JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.alfresco.error.AlfrescoRuntimeException;
24 import org.alfresco.service.cmr.repository.ContentIOException;
25 import org.alfresco.service.cmr.repository.ContentReader;
26 import org.alfresco.util.GUID;
27
28 /**
29  * Base class providing support for different types of content stores.
30  * <p>
31  * Since content URLs have to be consistent across all stores for
32  * reasons of replication and backup, the most important functionality
33  * provided is the generation of new content URLs and the checking of
34  * existing URLs.
35  *
36  * @author Derek Hulley
37  */

38 public abstract class AbstractContentStore implements ContentStore
39 {
40     /**
41      * Simple implementation that uses the
42      * {@link ContentReader#exists() reader's exists} method as its implementation.
43      */

44     public boolean exists(String JavaDoc contentUrl) throws ContentIOException
45     {
46         ContentReader reader = getReader(contentUrl);
47         return reader.exists();
48     }
49
50     /**
51      * Creates a new content URL. This must be supported by all
52      * stores that are compatible with Alfresco.
53      *
54      * @return Returns a new and unique content URL
55      */

56     public static String JavaDoc createNewUrl()
57     {
58         Calendar JavaDoc calendar = new GregorianCalendar JavaDoc();
59         int year = calendar.get(Calendar.YEAR);
60         int month = calendar.get(Calendar.MONTH) + 1; // 0-based
61
int day = calendar.get(Calendar.DAY_OF_MONTH);
62         int hour = calendar.get(Calendar.HOUR_OF_DAY);
63         // create the URL
64
StringBuilder JavaDoc sb = new StringBuilder JavaDoc(20);
65         sb.append(STORE_PROTOCOL)
66           .append(year).append('/')
67           .append(month).append('/')
68           .append(day).append('/')
69           .append(hour).append('/')
70           .append(GUID.generate()).append(".bin");
71         String JavaDoc newContentUrl = sb.toString();
72         // done
73
return newContentUrl;
74     }
75     
76     /**
77      * This method can be used to ensure that URLs conform to the
78      * required format. If subclasses have to parse the URL,
79      * then a call to this may not be required - provided that
80      * the format is checked.
81      * <p>
82      * The protocol part of the URL (including legacy protocols)
83      * is stripped out and just the relative path is returned.
84      *
85      * @param contentUrl a URL of the content to check
86      * @return Returns the relative part of the URL
87      * @throws RuntimeException if the URL is not correct
88      */

89     public static String JavaDoc getRelativePart(String JavaDoc contentUrl) throws RuntimeException JavaDoc
90     {
91         int index = 0;
92         if (contentUrl.startsWith(STORE_PROTOCOL))
93         {
94             index = 8;
95         }
96         else if (contentUrl.startsWith("file://"))
97         {
98             index = 7;
99         }
100         else
101         {
102             throw new AlfrescoRuntimeException(
103                     "All content URLs must start with " + STORE_PROTOCOL + ": \n" +
104                     " the invalid url is: " + contentUrl);
105         }
106         
107         // extract the relative part of the URL
108
String JavaDoc path = contentUrl.substring(index);
109         // more extensive checks can be added in, but it seems overkill
110
if (path.length() < 10)
111         {
112             throw new AlfrescoRuntimeException(
113                     "The content URL is invalid: \n" +
114                     " content url: " + contentUrl);
115         }
116         return path;
117     }
118
119     public final Set JavaDoc<String JavaDoc> getUrls() throws ContentIOException
120     {
121         return getUrls(null, null);
122     }
123 }
124
Popular Tags