Source Code Cross Referenced for KeyStoreLoaderSaver.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » tools » toolutils » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.tools.toolutils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements.  See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership.  The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License.  You may obtain a copy of the License at
009:         *
010:         *   http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied.  See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.    
018:         */
019:
020:        package org.apache.harmony.tools.toolutils;
021:
022:        import java.io.BufferedInputStream;
023:        import java.io.BufferedOutputStream;
024:        import java.io.File;
025:        import java.io.FileInputStream;
026:        import java.io.FileNotFoundException;
027:        import java.io.FileOutputStream;
028:        import java.io.IOException;
029:        import java.net.URI;
030:        import java.net.URISyntaxException;
031:        import java.security.KeyStore;
032:        import java.security.KeyStoreException;
033:        import java.security.NoSuchAlgorithmException;
034:        import java.security.NoSuchProviderException;
035:        import java.security.cert.CertificateException;
036:
037:        import org.apache.harmony.tools.keytool.KeytoolParameters;
038:
039:        /**
040:         * Class for loading and saving keystores.
041:         */
042:        public class KeyStoreLoaderSaver {
043:
044:            /**
045:             * Creates an instance of class KeyStore and loads a keystore of the
046:             * specified type to it. If the type is not specified the default one is
047:             * used. Password storePass is used to check the integrity of the keystore.
048:             * If the password is null, the integrity is not checked. If the path to the
049:             * store is not defined, an empty keystore is created.
050:             * 
051:             * @param path
052:             * @param storeType
053:             * @param storePass
054:             * @param providerName
055:             * @return
056:             * @throws FileNotFoundException
057:             * @throws KeyStoreException
058:             * @throws NoSuchAlgorithmException
059:             * @throws CertificateException
060:             * @throws IOException
061:             * @throws NoSuchProviderException
062:             */
063:            public static KeyStore loadStore(String path, String storeType,
064:                    char[] storePass, String providerName)
065:                    throws FileNotFoundException, KeyStoreException,
066:                    NoSuchAlgorithmException, CertificateException,
067:                    IOException, NoSuchProviderException {
068:
069:                BufferedInputStream bis;
070:                // if the path is given, make a FileInputStream on it
071:                if (path != null) {
072:                    File ksFile = null;
073:                    URI uri = null;
074:                    try {
075:                        uri = new URI(path);
076:                        ksFile = new File(uri);
077:                    } catch (URISyntaxException e) {
078:                        ksFile = new File(path);
079:                    } catch (IllegalArgumentException e) {
080:                        ksFile = new File(path);
081:                    }
082:
083:                    if (!ksFile.exists()) {
084:                        throw new IOException("Keystore file does not exist");
085:                    }
086:                    if (ksFile.length() == 0) {
087:                        throw new IOException(
088:                                "Keystore file exists but is empty");
089:                    }
090:                    bis = new BufferedInputStream(new FileInputStream(ksFile));
091:                } else { // if the path is not given, a new keystore will be created
092:                    bis = null;
093:                }
094:
095:                // Set the store type to default if it is not given.
096:                // The default value is set up in java.security file.
097:                String checkedStoreType = (storeType != null) ? storeType
098:                        : KeyStore.getDefaultType();
099:                KeyStore keyStore;
100:                // if the provider name is not set
101:                if (providerName == null) {
102:                    keyStore = KeyStore.getInstance(checkedStoreType);
103:                } else {
104:                    try {
105:                        keyStore = KeyStore
106:                                .getInstance(storeType, providerName);
107:                    } catch (NoSuchProviderException e) {
108:                        throw (NoSuchProviderException) new NoSuchProviderException(
109:                                "The provider " + providerName
110:                                        + " is not found in the environment.")
111:                                .initCause(e);
112:                    }
113:                }
114:
115:                try {
116:                    // try to load the keystore
117:                    keyStore.load(bis, storePass);
118:                } catch (NoSuchAlgorithmException e) {
119:                    throw new NoSuchAlgorithmException(
120:                            "Failed to find the algorithm to check the keystore integrity",
121:                            e);
122:                } catch (CertificateException e) {
123:                    throw new CertificateException(
124:                            "Failed to load a certificate from the keystore. ",
125:                            e);
126:                } catch (IOException e) {
127:                    throw (IOException) new IOException(
128:                            "Failed to load the keystore. ").initCause(e);
129:                } finally {
130:                    if (bis != null) {
131:                        bis.close();
132:                    }
133:                }
134:                return keyStore;
135:            }
136:
137:            /**
138:             * Saves a keystore to the file and protects its integrity with password.
139:             * 
140:             * @throws KeyStoreException
141:             * @throws NoSuchAlgorithmException
142:             * @throws CertificateException
143:             * @throws IOException
144:             */
145:            public static void saveStore(KeyStore keyStore, String storePath,
146:                    char[] storePass, boolean isVerbose)
147:                    throws KeyStoreException, NoSuchAlgorithmException,
148:                    CertificateException, IOException {
149:                // TODO: store not only to a file?
150:
151:                // if the program should output additional information, do it
152:                if (isVerbose) {
153:                    System.out.println("[Saving " + storePath + "]");
154:                }
155:
156:                // if the path to the store is not set, use the default value
157:                if (storePath == null) {
158:                    storePath = KeytoolParameters.defaultKeystorePath;
159:                }
160:
161:                File ksFile = null;
162:                URI uri = null;
163:                try {
164:                    uri = new URI(storePath);
165:                    ksFile = new File(uri);
166:                } catch (URISyntaxException e) {
167:                    ksFile = new File(storePath);
168:                } catch (IllegalArgumentException e) {
169:                    ksFile = new File(storePath);
170:                }
171:
172:                // the file will be created if and only if one with the same name
173:                // doesn't exist
174:                ksFile.createNewFile();
175:                BufferedOutputStream bos = new BufferedOutputStream(
176:                        new FileOutputStream(ksFile));
177:                try {
178:                    keyStore.store(bos, storePass);
179:                } catch (NoSuchAlgorithmException e) {
180:                    throw new NoSuchAlgorithmException(
181:                            "Failed to find the algorithm to check the keystore integrity",
182:                            e);
183:                } catch (CertificateException e) {
184:                    throw new CertificateException(
185:                            "Failed to save a certificate to the keystore. ", e);
186:                } catch (IOException e) {
187:                    throw (IOException) new IOException(
188:                            "Failed to save the keystore. ").initCause(e);
189:                } finally {
190:                    bos.close();
191:                }
192:            }
193:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.