| 1 | /*- |
|---|
| 2 | * Copyright (c) 2006, Derek Konigsberg |
|---|
| 3 | * All rights reserved. |
|---|
| 4 | * |
|---|
| 5 | * Redistribution and use in source and binary forms, with or without |
|---|
| 6 | * modification, are permitted provided that the following conditions |
|---|
| 7 | * are met: |
|---|
| 8 | * |
|---|
| 9 | * 1. Redistributions of source code must retain the above copyright |
|---|
| 10 | * notice, this list of conditions and the following disclaimer. |
|---|
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the |
|---|
| 13 | * documentation and/or other materials provided with the distribution. |
|---|
| 14 | * 3. Neither the name of the project nor the names of its |
|---|
| 15 | * contributors may be used to endorse or promote products derived |
|---|
| 16 | * from this software without specific prior written permission. |
|---|
| 17 | * |
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|---|
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|---|
| 22 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|---|
| 24 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|---|
| 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|---|
| 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|---|
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
|---|
| 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 30 | */ |
|---|
| 31 | |
|---|
| 32 | package org.logicprobe.LogicMail.util; |
|---|
| 33 | |
|---|
| 34 | import java.io.IOException; |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * Proxy for utility classes and methods that may |
|---|
| 38 | * have multiple implementations depending on the |
|---|
| 39 | * project build configuration. |
|---|
| 40 | */ |
|---|
| 41 | public abstract class UtilProxy { |
|---|
| 42 | private static UtilProxy instance = null; |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * Get a class reference for the util proxy, using exception catching |
|---|
| 46 | * to determine which one this build has been compiled with. |
|---|
| 47 | */ |
|---|
| 48 | private static Class getUtilProxyClass() { |
|---|
| 49 | Class utilProxyClass; |
|---|
| 50 | try { |
|---|
| 51 | utilProxyClass = Class.forName("org.logicprobe.LogicMail.util.UtilProxyBB41"); |
|---|
| 52 | return utilProxyClass; |
|---|
| 53 | } |
|---|
| 54 | catch (ClassNotFoundException e) { } |
|---|
| 55 | try { |
|---|
| 56 | utilProxyClass = Class.forName("org.logicprobe.LogicMail.util.UtilProxyBB40"); |
|---|
| 57 | return utilProxyClass; |
|---|
| 58 | } |
|---|
| 59 | catch (ClassNotFoundException e) { } |
|---|
| 60 | return null; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * Creates a new instance of UtilProxy |
|---|
| 65 | */ |
|---|
| 66 | private static UtilProxy createUtilProxy() { |
|---|
| 67 | UtilProxy utilProxy = null; |
|---|
| 68 | Class utilProxyClass = getUtilProxyClass(); |
|---|
| 69 | if(utilProxyClass == null) { |
|---|
| 70 | return null; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | try { |
|---|
| 74 | utilProxy = (UtilProxy)utilProxyClass.newInstance(); |
|---|
| 75 | } catch (InstantiationException e) { |
|---|
| 76 | utilProxy = null; |
|---|
| 77 | } catch (IllegalAccessException e) { |
|---|
| 78 | utilProxy = null; |
|---|
| 79 | } |
|---|
| 80 | return utilProxy; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | public static synchronized UtilProxy getInstance() { |
|---|
| 84 | if(instance == null) { |
|---|
| 85 | instance = createUtilProxy(); |
|---|
| 86 | if(instance == null) { |
|---|
| 87 | throw new RuntimeException("Application configuration error"); |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | return instance; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * Decode the Base64 encoded input and return the result. |
|---|
| 96 | * |
|---|
| 97 | * @param input The Base64 encoded input |
|---|
| 98 | * @return A byte array containing the decoded input. |
|---|
| 99 | * @throws IOException Thrown if a decoding error occurred. |
|---|
| 100 | */ |
|---|
| 101 | public abstract byte[] Base64Decode(String input) throws IOException; |
|---|
| 102 | |
|---|
| 103 | /** |
|---|
| 104 | * Encodes the provided input into Base 64 and returns the encoded result. |
|---|
| 105 | * |
|---|
| 106 | * @param input The input data to encode |
|---|
| 107 | * @param inputOffset The offset into the array |
|---|
| 108 | * @param inputLength The length of the array |
|---|
| 109 | * @param insertCR Set to true if you want to insert a CR after every 76th encoded character |
|---|
| 110 | * @param insertLF Set to true if you want to insert a LF after every 76th encoded character |
|---|
| 111 | * @throws IOException Thrown if an encoding error occurred |
|---|
| 112 | * @return The encoded input as a byte array |
|---|
| 113 | */ |
|---|
| 114 | public abstract byte[] Base64Encode(byte[] input, int inputOffset, int inputLength, boolean insertCR, boolean insertLF) throws IOException; |
|---|
| 115 | |
|---|
| 116 | /** |
|---|
| 117 | * Encodes the provided input into Base 64 and returns the encoded result. |
|---|
| 118 | * |
|---|
| 119 | * @param input The input data to encode |
|---|
| 120 | * @param inputOffset The offset into the array |
|---|
| 121 | * @param inputLength The length of the array |
|---|
| 122 | * @param insertCR Set to true if you want to insert a CR after every 76th encoded character |
|---|
| 123 | * @param insertLF Set to true if you want to insert a LF after every 76th encoded character |
|---|
| 124 | * @throws IOException Thrown if an encoding error occurred |
|---|
| 125 | * @return The encoded input as a string |
|---|
| 126 | */ |
|---|
| 127 | public abstract String Base64EncodeAsString(byte[] input, int inputOffset, int inputLength, boolean insertCR, boolean insertLF) throws IOException; |
|---|
| 128 | } |
|---|