Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 Google Inc. | 2 * Copyright (c) 2012 Google Inc. |
3 * | 3 * |
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not u se this file except | 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not u se this file except |
5 * in compliance with the License. You may obtain a copy of the License at | 5 * in compliance with the License. You may obtain a copy of the License at |
6 * | 6 * |
7 * http://www.apache.org/licenses/LICENSE-2.0 | 7 * http://www.apache.org/licenses/LICENSE-2.0 |
8 * | 8 * |
9 * Unless required by applicable law or agreed to in writing, software distribut ed under the License | 9 * Unless required by applicable law or agreed to in writing, software distribut ed under the License |
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K IND, either express | 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K IND, either express |
11 * or implied. See the License for the specific language governing permissions a nd limitations under | 11 * or implied. See the License for the specific language governing permissions a nd limitations under |
12 * the License. | 12 * the License. |
13 */ | 13 */ |
14 | 14 |
15 package com.google.api.client.extensions.java6.auth.oauth2; | 15 package com.google.api.client.extensions.java6.auth.oauth2; |
16 | 16 |
17 import com.google.api.client.auth.oauth2.Credential; | |
18 import com.google.api.client.auth.oauth2.CredentialStore; | |
19 import com.google.api.client.json.JsonFactory; | |
20 import com.google.api.client.json.JsonGenerator; | |
21 import com.google.common.base.Charsets; | |
22 import com.google.common.base.Preconditions; | |
23 | |
17 import java.io.File; | 24 import java.io.File; |
18 import java.io.FileInputStream; | 25 import java.io.FileInputStream; |
19 import java.io.FileOutputStream; | 26 import java.io.FileOutputStream; |
20 import java.io.IOException; | 27 import java.io.IOException; |
21 import java.util.concurrent.locks.Lock; | 28 import java.util.concurrent.locks.Lock; |
22 import java.util.concurrent.locks.ReentrantLock; | 29 import java.util.concurrent.locks.ReentrantLock; |
23 | |
24 import com.google.api.client.auth.oauth2.Credential; | |
25 import com.google.api.client.auth.oauth2.CredentialStore; | |
26 import com.google.api.client.json.JsonFactory; | |
27 import com.google.api.client.json.JsonGenerator; | |
28 import com.google.common.base.Charsets; | |
29 import com.google.common.base.Preconditions; | |
30 | 30 |
31 /** | 31 /** |
32 * Thread-safe file implementation of a credential store. | 32 * Thread-safe file implementation of a credential store. |
33 *· | 33 *· |
34 * @since 1.11 | 34 * @since 1.11 |
35 * @author Rafael Naufal | 35 * @author Rafael Naufal |
36 */ | 36 */ |
37 public class FileCredentialStore implements CredentialStore { | 37 public class FileCredentialStore implements CredentialStore { |
yanivi
2012/07/11 14:58:15
$mvn clean install
/usr/local/google/users/yanivi
rafael.naufal
2012/07/11 21:44:33
Done.
| |
38 | 38 |
39 /** Json factory for serializing user credentials. */ | 39 /** Json factory for serializing user credentials. */ |
40 private final JsonFactory jsonFactory; | 40 private final JsonFactory jsonFactory; |
41 | 41 |
42 /** Lock on access to the store. */ | 42 /** Lock on access to the store. */ |
43 private final Lock lock = new ReentrantLock(); | 43 private final Lock lock = new ReentrantLock(); |
44 | 44 |
45 /** User credentials repository. */ | 45 /** User credentials repository. */ |
46 private FilePersistedCredentials credentials = new FilePersistedCredentials( ); | 46 private FilePersistedCredentials credentials = new FilePersistedCredentials(); |
47 | 47 |
48 /** File to store user credentials. */ | 48 /** File to store user credentials. */ |
49 private final File file; | 49 private final File file; |
50 | 50 |
51 /** | 51 /** |
52 *· | 52 *· |
53 * @param file | 53 * @param file |
54 * File to store user credentials | 54 * File to store user credentials |
55 * @param jsonFactory | 55 * @param jsonFactory |
56 * JSON factory to serialize user credentials | 56 * JSON factory to serialize user credentials |
57 */ | 57 */ |
58 public FileCredentialStore(File file, JsonFactory jsonFactory) | 58 public FileCredentialStore(File file, JsonFactory jsonFactory) throws IOExcept ion { |
yanivi
2012/07/11 14:58:15
We are using 2-space indent in this project instea
rafael.naufal
2012/07/11 21:44:33
Done.
| |
59 throws IOException { | 59 this.file = Preconditions.checkNotNull(file, "missing file for credentials") ; |
60 this.file = Preconditions.checkNotNull(file, | 60 this.jsonFactory = Preconditions.checkNotNull(jsonFactory, "missing json fac tory"); |
61 "missing file for credentials"); | 61 if (file.exists()) { |
62 this.jsonFactory = Preconditions.checkNotNull(jsonFactory, | 62 loadCredentials(file); |
63 "missing json factory"); | 63 } else { |
64 if (file.exists()) { | 64 file.setReadable(false, false); |
65 loadCredentials(file); | 65 Preconditions.checkState(file.setWritable(false, false), |
66 } else { | 66 "Error when setting permissions for %s", file); |
67 file.setReadable(false, false); | 67 file.setExecutable(false, false); |
68 file.setWritable(false, false); | 68 file.setReadable(true); |
69 file.setExecutable(false, false); | 69 file.setWritable(true); |
70 file.setReadable(true); | |
71 file.setWritable(true); | |
72 } | |
73 } | 70 } |
71 } | |
74 | 72 |
75 public void store(String userId, Credential credential) throws IOException { | 73 @Override |
yanivi
2012/07/11 14:58:15
Description Resource Path Location Type
The method
rafael.naufal
2012/07/11 21:44:33
Done.
| |
76 lock.lock(); | 74 public void store(String userId, Credential credential) throws IOException { |
77 try { | 75 lock.lock(); |
78 credentials.store(userId, credential); | 76 try { |
79 writeCredentials(userId, credential); | 77 credentials.store(userId, credential); |
80 } finally { | 78 writeCredentials(userId, credential); |
81 lock.unlock(); | 79 } finally { |
82 } | 80 lock.unlock(); |
83 } | 81 } |
82 } | |
84 | 83 |
85 public void delete(String userId, Credential credential) throws IOException { | 84 @Override |
86 lock.lock(); | 85 public void delete(String userId, Credential credential) throws IOException { |
87 try { | 86 lock.lock(); |
88 credentials.delete(userId); | 87 try { |
89 writeCredentials(userId, credential); | 88 credentials.delete(userId); |
90 } finally { | 89 writeCredentials(userId, credential); |
91 lock.unlock(); | 90 } finally { |
92 } | 91 lock.unlock(); |
93 } | 92 } |
93 } | |
94 | 94 |
95 public boolean load(String userId, Credential credential) { | 95 @Override |
96 lock.lock(); | 96 public boolean load(String userId, Credential credential) { |
97 try { | 97 lock.lock(); |
98 return credentials.load(userId, credential); | 98 try { |
99 } finally { | 99 return credentials.load(userId, credential); |
100 lock.unlock(); | 100 } finally { |
101 } | 101 lock.unlock(); |
102 } | 102 } |
103 } | |
103 | 104 |
104 private void loadCredentials(File file) throws IOException { | 105 private void loadCredentials(File file) throws IOException { |
105 FileInputStream is = new FileInputStream(file); | 106 FileInputStream is = new FileInputStream(file); |
106 try { | 107 try { |
107 this.credentials = this.jsonFactory.fromInputStream(is, | 108 this.credentials = this.jsonFactory.fromInputStream(is, FilePersistedCrede ntials.class); |
108 FilePersistedCredentials.class); | 109 } finally { |
109 } finally { | 110 is.close(); |
110 is.close(); | |
111 } | |
112 } | 111 } |
112 } | |
113 | 113 |
114 private void writeCredentials(String userId, Credential credential) | 114 private void writeCredentials(String userId, Credential credential) throws IOE xception { |
yanivi
2012/07/11 14:58:15
[optional] We are using 100 space line length, not
rafael.naufal
2012/07/11 21:44:33
Done.
| |
115 throws IOException { | 115 FileOutputStream fos = new FileOutputStream(file); |
116 FileOutputStream fos = null; | 116 try { |
yanivi
2012/07/11 14:58:15
better to initialize here:
FileOutputStre
rafael.naufal
2012/07/11 21:44:33
Done.
| |
117 try { | 117 JsonGenerator generator = jsonFactory.createJsonGenerator(fos, Charsets.UT F_8); |
118 fos = new FileOutputStream(file); | 118 generator.serialize(credentials); |
119 JsonGenerator generator = jsonFactory.createJsonGenerator(fos, | 119 generator.close(); |
120 Charsets.UTF_8); | 120 } finally { |
121 generator.serialize(credentials); | 121 fos.close(); |
122 generator.close(); | |
123 } finally { | |
124 if (fos != null) { | |
125 fos.close(); | |
126 } | |
127 } | |
128 } | 122 } |
123 } | |
129 } | 124 } |
LEFT | RIGHT |