| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kew.api; |
| 17 | |
|
| 18 | |
import java.io.BufferedReader; |
| 19 | |
import java.io.IOException; |
| 20 | |
import java.io.InputStream; |
| 21 | |
import java.io.InputStreamReader; |
| 22 | |
import java.lang.reflect.InvocationTargetException; |
| 23 | |
import java.lang.reflect.Method; |
| 24 | |
|
| 25 | |
import org.apache.commons.lang.StringUtils; |
| 26 | |
import org.kuali.rice.core.api.config.ConfigurationException; |
| 27 | |
import org.kuali.rice.core.util.ClassLoaderUtils; |
| 28 | |
import org.kuali.rice.kew.api.action.InvalidActionTakenException; |
| 29 | |
import org.kuali.rice.kew.api.doctype.IllegalDocumentTypeException; |
| 30 | |
import org.kuali.rice.kew.api.document.DocumentContentUpdate; |
| 31 | |
import org.kuali.rice.kew.api.document.DocumentUpdate; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | 0 | public final class WorkflowDocumentFactory { |
| 38 | |
|
| 39 | |
private static final String CREATE_METHOD_NAME = "createDocument"; |
| 40 | |
private static final String LOAD_METHOD_NAME = "loadDocument"; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public static WorkflowDocument createDocument(String principalId, String documentTypeName) { |
| 57 | 0 | return createDocument(principalId, documentTypeName, null, null); |
| 58 | |
} |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
public static WorkflowDocument createDocument(String principalId, String documentTypeName, String title) { |
| 74 | 0 | DocumentUpdate.Builder builder = DocumentUpdate.Builder.create(); |
| 75 | 0 | builder.setTitle(title); |
| 76 | 0 | return createDocument(principalId, documentTypeName, builder.build(), null); |
| 77 | |
} |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
public static WorkflowDocument createDocument(String principalId, String documentTypeName, DocumentUpdate documentUpdate, DocumentContentUpdate documentContentUpdate) { |
| 94 | 0 | if (StringUtils.isBlank(principalId)) { |
| 95 | 0 | throw new IllegalArgumentException("principalId was null or blank"); |
| 96 | |
} |
| 97 | 0 | if (StringUtils.isBlank(documentTypeName)) { |
| 98 | 0 | throw new IllegalArgumentException("documentTypeName was null or blank"); |
| 99 | |
} |
| 100 | 0 | Object provider = loadProvider(); |
| 101 | 0 | Method createMethod = null; |
| 102 | |
try { |
| 103 | 0 | createMethod = provider.getClass().getMethod(CREATE_METHOD_NAME, String.class, String.class, DocumentUpdate.class, DocumentContentUpdate.class); |
| 104 | 0 | } catch (NoSuchMethodException e) { |
| 105 | 0 | throw new ConfigurationException("Failed to locate valid createDocument method signature on provider class: " + provider.getClass().getName(), e); |
| 106 | 0 | } catch (SecurityException e) { |
| 107 | 0 | throw new ConfigurationException("Encountered security issue when attempting to access createDocument method on provider class: " + provider.getClass().getName(), e); |
| 108 | 0 | } |
| 109 | |
|
| 110 | 0 | Object workflowDocument = null; |
| 111 | |
|
| 112 | |
try { |
| 113 | 0 | workflowDocument = createMethod.invoke(provider, principalId, documentTypeName, documentUpdate, documentContentUpdate); |
| 114 | 0 | } catch (IllegalAccessException e) { |
| 115 | 0 | throw new ConfigurationException("Failed to invoke " + CREATE_METHOD_NAME, e); |
| 116 | 0 | } catch (InvocationTargetException e) { |
| 117 | 0 | if (e.getCause() instanceof RuntimeException) { |
| 118 | 0 | throw (RuntimeException)e.getCause(); |
| 119 | |
} |
| 120 | 0 | throw new ConfigurationException("Failed to invoke " + CREATE_METHOD_NAME, e); |
| 121 | 0 | } |
| 122 | |
|
| 123 | 0 | if (!(workflowDocument instanceof WorkflowDocument)) { |
| 124 | 0 | throw new ConfigurationException("Created document is not a proper instance of " + WorkflowDocument.class + ", was instead " + workflowDocument.getClass()); |
| 125 | |
} |
| 126 | 0 | return (WorkflowDocument)workflowDocument; |
| 127 | |
} |
| 128 | |
|
| 129 | |
public static WorkflowDocument loadDocument(String principalId, String documentId) { |
| 130 | 0 | if (StringUtils.isBlank(principalId)) { |
| 131 | 0 | throw new IllegalArgumentException("principalId was null or blank"); |
| 132 | |
} |
| 133 | 0 | if (StringUtils.isBlank(documentId)) { |
| 134 | 0 | throw new IllegalArgumentException("documentId was null or blank"); |
| 135 | |
} |
| 136 | |
|
| 137 | 0 | Object provider = loadProvider(); |
| 138 | 0 | Method loadMethod = null; |
| 139 | |
try { |
| 140 | 0 | loadMethod = provider.getClass().getMethod(LOAD_METHOD_NAME, String.class, String.class); |
| 141 | 0 | } catch (NoSuchMethodException e) { |
| 142 | 0 | throw new ConfigurationException("Failed to locate valid createDocument method signature on provider class: " + provider.getClass().getName(), e); |
| 143 | 0 | } catch (SecurityException e) { |
| 144 | 0 | throw new ConfigurationException("Encountered security issue when attempting to access createDocument method on provider class: " + provider.getClass().getName(), e); |
| 145 | 0 | } |
| 146 | |
|
| 147 | 0 | Object workflowDocument = null; |
| 148 | |
|
| 149 | |
try { |
| 150 | 0 | workflowDocument = loadMethod.invoke(provider, principalId, documentId); |
| 151 | 0 | } catch (IllegalAccessException e) { |
| 152 | 0 | throw new ConfigurationException("Failed to invoke " + LOAD_METHOD_NAME, e); |
| 153 | 0 | } catch (InvocationTargetException e) { |
| 154 | 0 | if (e.getCause() instanceof RuntimeException) { |
| 155 | 0 | throw (RuntimeException)e.getCause(); |
| 156 | |
} |
| 157 | 0 | throw new ConfigurationException("Failed to invoke " + LOAD_METHOD_NAME, e); |
| 158 | 0 | } |
| 159 | |
|
| 160 | 0 | if (!(workflowDocument instanceof WorkflowDocument)) { |
| 161 | 0 | throw new ConfigurationException("Loaded document is not a proper instance of " + WorkflowDocument.class + ", was instead " + workflowDocument.getClass()); |
| 162 | |
} |
| 163 | 0 | return (WorkflowDocument)workflowDocument; |
| 164 | |
} |
| 165 | |
|
| 166 | |
private static Object loadProvider() { |
| 167 | 0 | String providerClassName = null; |
| 168 | 0 | String resource = null; |
| 169 | |
try { |
| 170 | 0 | resource = new StringBuilder().append("META-INF/services/").append(WorkflowDocument.class.getName()).toString(); |
| 171 | 0 | final InputStream resourceStream = ClassLoaderUtils.getDefaultClassLoader().getResourceAsStream(resource.toString()); |
| 172 | 0 | if (resourceStream != null) { |
| 173 | 0 | BufferedReader reader = new BufferedReader(new InputStreamReader(resourceStream, "UTF-8")); |
| 174 | 0 | providerClassName = reader.readLine().trim(); |
| 175 | 0 | reader.close(); |
| 176 | 0 | Class<?> providerClass = Class.forName(providerClassName); |
| 177 | 0 | return newInstance(providerClass); |
| 178 | |
} else { |
| 179 | 0 | throw new ConfigurationException("Failed to locate a services definition file at " + resource); |
| 180 | |
} |
| 181 | 0 | } catch (IOException e) { |
| 182 | 0 | throw new ConfigurationException("Failure processing services definition file at " + resource, e); |
| 183 | 0 | } catch (ClassNotFoundException e) { |
| 184 | 0 | throw new ConfigurationException("Failed to load provider class: " + providerClassName, e); |
| 185 | |
} |
| 186 | |
} |
| 187 | |
|
| 188 | |
private static Object newInstance(Class<?> providerClass) { |
| 189 | |
try { |
| 190 | 0 | return providerClass.newInstance(); |
| 191 | 0 | } catch (InstantiationException e) { |
| 192 | 0 | throw new ConfigurationException("Failed to instantiate provider class: " + providerClass.getName(), e); |
| 193 | 0 | } catch (IllegalAccessException e) { |
| 194 | 0 | throw new ConfigurationException("Failed to instantiate provider class: " + providerClass.getName(), e); |
| 195 | |
} |
| 196 | |
} |
| 197 | |
} |