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

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   *
20   */
21  package org.apache.chemistry.opencmis.client.bindings.spi.local;
22  
23  import java.io.File;
24  import java.math.BigInteger;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
29  import org.apache.chemistry.opencmis.commons.SessionParameter;
30  import org.apache.chemistry.opencmis.commons.server.CallContext;
31  import org.apache.chemistry.opencmis.commons.server.CmisService;
32  import org.apache.chemistry.opencmis.commons.server.CmisServiceFactory;
33  
34  /**
35   * Base class for all local clients.
36   */
37  public abstract class AbstractLocalService {
38  
39      private BindingSession session;
40      private CmisServiceFactory factory;
41  
42      private String user;
43      private String password;
44  
45      /**
46       * Sets the current session.
47       */
48      protected void setSession(BindingSession session) {
49          this.session = session;
50  
51          Object userObj = session.get(SessionParameter.USER);
52          user = userObj instanceof String ? userObj.toString() : null;
53  
54          Object passwordObj = session.get(SessionParameter.PASSWORD);
55          password = passwordObj instanceof String ? passwordObj.toString() : null;
56      }
57  
58      /**
59       * Gets the current session.
60       */
61      protected BindingSession getSession() {
62          return session;
63      }
64  
65      /**
66       * Sets the service factory.
67       */
68      protected void setServiceFactory(CmisServiceFactory factory) {
69          this.factory = factory;
70      }
71  
72      /**
73       * Gets the service factory.
74       */
75      protected CmisServiceFactory getServiceFactory() {
76          return factory;
77      }
78  
79      /**
80       * creates a local call context.
81       */
82      protected CallContext createCallContext(String repositoryId) {
83          return new LocalCallContext(repositoryId, user, password);
84      }
85  
86      protected CmisService getService(String repositoryId) {
87          return factory.getService(createCallContext(repositoryId));
88      }
89  
90      // ------------------------------------------------------------------
91  
92      /**
93       * Simple {@link CallContext} implementation.
94       */
95      static class LocalCallContext implements CallContext {
96  
97          private final Map<String, Object> contextMap = new HashMap<String, Object>();
98  
99          public LocalCallContext(String repositoryId, String user, String password) {
100             contextMap.put(REPOSITORY_ID, repositoryId);
101             contextMap.put(USERNAME, user);
102             contextMap.put(PASSWORD, password);
103         }
104 
105         public String getBinding() {
106             return BINDING_LOCAL;
107         }
108 
109         public Object get(String key) {
110             return contextMap.get(key);
111         }
112 
113         public String getRepositoryId() {
114             return (String) get(REPOSITORY_ID);
115         }
116 
117         public String getUsername() {
118             return (String) get(USERNAME);
119         }
120 
121         public String getPassword() {
122             return (String) get(PASSWORD);
123         }
124 
125         public String getLocale() {
126             return null;
127         }
128 
129         public BigInteger getOffset() {
130             return (BigInteger) get(OFFSET);
131         }
132 
133         public BigInteger getLength() {
134             return (BigInteger) get(LENGTH);
135         }
136 
137         public boolean isObjectInfoRequired() {
138             return false;
139         }
140 
141         public File getTempDirectory() {
142             return null;
143         }
144 
145         public int getMemoryThreshold() {
146             return 0;
147         }
148     }
149 }