01: /**
02: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
03: *
04: * Permission is hereby granted, free of charge, to any person obtaining a copy
05: * of this software and associated documentation files (the "Software"), to deal
06: * in the Software without restriction, including without limitation the rights
07: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
08: * copies of the Software, and to permit persons to whom the Software is
09: * furnished to do so, subject to the following conditions:
10: *
11: * The above copyright notice and this permission notice shall be included in
12: * all copies or substantial portions of the Software.
13: *
14: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20: * SOFTWARE.
21: */package com.liferay.portlet.amazonrankings.util;
22:
23: import com.liferay.portal.kernel.util.StringPool;
24: import com.liferay.portal.kernel.webcache.WebCacheItem;
25: import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
26: import com.liferay.portal.util.PropsUtil;
27: import com.liferay.portlet.amazonrankings.model.AmazonRankings;
28:
29: import java.util.List;
30: import java.util.Vector;
31:
32: /**
33: * <a href="AmazonRankingsUtil.java.html"><b><i>View Source</i></b></a>
34: *
35: * @author Brian Wing Shun Chan
36: *
37: */
38: public class AmazonRankingsUtil {
39:
40: public static String getAmazonKey() {
41: return _instance._getAmazonKey();
42: }
43:
44: public static AmazonRankings getAmazonRankings(String isbn) {
45: WebCacheItem wci = new AmazonRankingsWebCacheItem(isbn);
46:
47: return (AmazonRankings) WebCachePoolUtil.get(
48: AmazonRankingsUtil.class.getName() + StringPool.PERIOD
49: + isbn, wci);
50: }
51:
52: private AmazonRankingsUtil() {
53: _amazonLicenseKeys = new Vector();
54:
55: for (int i = 0; i < 1000; i++) {
56: String key = PropsUtil.get(PropsUtil.AMAZON_LICENSE + i);
57:
58: if (key == null) {
59: break;
60: } else {
61: _amazonLicenseKeys.add(key);
62: }
63: }
64: }
65:
66: private String _getAmazonKey() {
67: if (_amazonLicenseKeys.size() > 0) {
68: _amazonLicenseCount++;
69:
70: if (_amazonLicenseCount >= _amazonLicenseKeys.size()) {
71: _amazonLicenseCount = 0;
72: }
73:
74: return (String) _amazonLicenseKeys.get(_amazonLicenseCount);
75: } else {
76: return null;
77: }
78: }
79:
80: private static AmazonRankingsUtil _instance = new AmazonRankingsUtil();
81:
82: private List _amazonLicenseKeys;
83: private int _amazonLicenseCount;
84:
85: }
|