This project has retired. For details please refer to its Attic page.
RepositoryUrlCache xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.client.bindings.spi.browser;
20  
21  import java.io.Serializable;
22  import java.util.HashMap;
23  import java.util.Map;
24  import java.util.concurrent.locks.ReentrantReadWriteLock;
25  
26  import org.apache.chemistry.opencmis.commons.impl.Constants;
27  import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
28  
29  /**
30   * URL cache for repository and root URLs.
31   */
32  public class RepositoryUrlCache implements Serializable {
33  
34      public static final String OBJECT_ID = "objectId";
35  
36      private static final long serialVersionUID = 1L;
37  
38      private final Map<String, String> repositoryUrls;
39      private final Map<String, String> rootUrls;
40  
41      private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
42  
43      public RepositoryUrlCache() {
44          repositoryUrls = new HashMap<String, String>();
45          rootUrls = new HashMap<String, String>();
46      }
47  
48      /**
49       * Adds the URLs of a repository to the cache.
50       */
51      public void addRepository(String repositoryId, String repositoryUrl, String rootUrl) {
52          if (repositoryId == null || repositoryUrl == null || rootUrl == null) {
53              throw new IllegalArgumentException("Repository Id or Repository URL or Root URL is not set!");
54          }
55  
56          lock.writeLock().lock();
57          try {
58              repositoryUrls.put(repositoryId, repositoryUrl);
59              rootUrls.put(repositoryId, rootUrl);
60          } finally {
61              lock.writeLock().unlock();
62          }
63      }
64  
65      /**
66       * Removes the URLs of a repository from the cache.
67       */
68      public void removeRepository(String repositoryId) {
69          lock.writeLock().lock();
70          try {
71              repositoryUrls.remove(repositoryId);
72              rootUrls.remove(repositoryId);
73          } finally {
74              lock.writeLock().unlock();
75          }
76      }
77  
78      /**
79       * Returns the base repository URL of a repository.
80       */
81      public String getRepositoryBaseUrl(String repositoryId) {
82          lock.readLock().lock();
83          try {
84              return repositoryUrls.get(repositoryId);
85          } finally {
86              lock.readLock().unlock();
87          }
88      }
89  
90      /**
91       * Returns the root URL of a repository.
92       */
93      public String getRootUrl(String repositoryId) {
94          lock.readLock().lock();
95          try {
96              return rootUrls.get(repositoryId);
97          } finally {
98              lock.readLock().unlock();
99          }
100     }
101 
102     /**
103      * Returns the repository URL.
104      */
105     public UrlBuilder getRepositoryUrl(String repositoryId) {
106         String base = getRepositoryBaseUrl(repositoryId);
107         if (base == null) {
108             return null;
109         }
110 
111         return new UrlBuilder(base);
112     }
113 
114     /**
115      * Returns the repository URL with the given selector.
116      */
117     public UrlBuilder getRepositoryUrl(String repositoryId, String selector) {
118         UrlBuilder result = getRepositoryUrl(repositoryId);
119         if (result == null) {
120             return null;
121         }
122 
123         result.addParameter(Constants.PARAM_SELECTOR, selector);
124 
125         return result;
126     }
127 
128     /**
129      * Returns an object URL with the given selector.
130      */
131     public UrlBuilder getObjectUrl(String repositoryId, String objectId) {
132         String root = getRootUrl(repositoryId);
133         if (root == null) {
134             return null;
135         }
136 
137         UrlBuilder result = new UrlBuilder(root);
138         result.addParameter(OBJECT_ID, objectId);
139 
140         return result;
141     }
142 
143     /**
144      * Returns an object URL with the given selector.
145      */
146     public UrlBuilder getObjectUrl(String repositoryId, String objectId, String selector) {
147         UrlBuilder result = getObjectUrl(repositoryId, objectId);
148         if (result == null) {
149             return null;
150         }
151 
152         result.addParameter(Constants.PARAM_SELECTOR, selector);
153 
154         return result;
155     }
156 
157     /**
158      * Returns an object URL with the given selector.
159      */
160     public UrlBuilder getPathUrl(String repositoryId, String path) {
161         String root = getRootUrl(repositoryId);
162         if (root == null) {
163             return null;
164         }
165 
166         UrlBuilder result = new UrlBuilder(root);
167         result.addPath(path);
168 
169         return result;
170     }
171 
172     /**
173      * Returns an object URL with the given selector.
174      */
175     public UrlBuilder getPathUrl(String repositoryId, String path, String selector) {
176         UrlBuilder result = getPathUrl(repositoryId, path);
177         if (result == null) {
178             return null;
179         }
180 
181         result.addParameter(Constants.PARAM_SELECTOR, selector);
182 
183         return result;
184     }
185 }