/** * Assert this instance is attached to a HTTP servlet request. * * @throws BugError if this instance is not attached. * @see #attached */ private void assertAttached() { if (!attached) { throw new BugError("Attempt to use request context outside HTTP request thread."); } } }
@Override public String getRequestPath() { if (!remotelyAccessible) { throw new BugError("Attempt to retrieve request URI path from local managed method |%s|.", this); } return requestPath; }
@Override public Class<?> getInterfaceClass() { if (interfaceClasses.length > 1) { throw new BugError("Attempt to treat multiple interfaces as a single one."); } return interfaceClasses[0]; }
@Override public String getRequestPath() { if (!remotelyAccessible) { throw new BugError("Attempt to retrive request URI path for local managed class |%s|.", this); } return requestPath; }
@Override public String handle(String variableName) { String property = System.getProperty(variableName); if(property == null) { throw new BugError("Missing system property |%s|. String |%s| variable injection aborted.", variableName, string); } return property; } });
/** * Cast Java reflective type to language class. If <code>type</code> is instance of {@link Class} just return it. If * is parameterized type returns the raw class. * * @param t Java reflective type. * @return the class described by given <code>type</code>. */ private static Class<?> typeToClass(Type t) { if(t instanceof Class<?>) { return (Class<?>)t; } if(t instanceof ParameterizedType) { return (Class<?>)((ParameterizedType)t).getRawType(); } throw new BugError("Unknown type %s to convert to class.", t); }
/** * Lookup implementation into given registry, throwing exception if not found. * * @param implementationsRegistry implementations registry, * @param interfaceType interface to lookup into registry. * @return implementation for requested interface type. * @throws BugError if implementation is not found into registry. */ public static Class<?> getImplementation(Map<Class<?>, Class<?>> implementationsRegistry, Type interfaceType) { Class<?> implementation = implementationsRegistry.get(interfaceType); if(implementation == null) { throw new BugError("No registered implementation for type |%s|.", interfaceType); } return implementation; }
/** * Persist instance on current thread bound to given managed class. This method simply uses provided instance key argument * to add instance to {@link #instancesPool}. Both arguments should to be not null. * * @param instanceKey managed instance key, * @param instance managed instance. */ @Override public void persistInstance(InstanceKey instanceKey, Object instance) { // at this point managed class and instance are guaranteed to be non null ThreadLocal<Object> tls = instancesPool.get(instanceKey); if (tls == null) { throw new BugError("Invalid methods invocation sequence. Ensure getInstance() is invoked before and is executed in the same thread."); } tls.set(instance); }
/** * Helper method to retrieve application factory bound to current thread. Retrieve application factory from current thread * local storage. In order to be successfully this method must be preceded by {@link #bind(AppFactory)} called from current * thread; otherwise bug error is thrown. * * @return application factory bound to current thread. * @throws BugError if current thread has no application factory bound. */ public static AppFactory getAppFactory() { AppFactory appFactory = tls.get(); if (appFactory == null) { throw new BugError("No application factory bound to current thread |%s|. See #bind(AppFactory).", Thread.currentThread()); } return appFactory; } }
/** * Create buffered writer from bytes stream using UTF-8 charset. This library always uses UTF-8 encoding and this * method does not rely on JVM default since there is no standard way to enforce it. JRE specification states that * <code>file.encoding</code> is not the standard way to set default charset and to use host settings. * * @param stream input stream. * @return newly created buffered reader. */ public static BufferedWriter createBufferedWriter(OutputStream stream) { try { return new BufferedWriter(new OutputStreamWriter(stream, "UTF-8")); } catch(UnsupportedEncodingException unused) { throw new BugError("Unsupported UTF-8 ecoding."); } }
/** * Create buffered reader from bytes stream using UTF-8 charset. This library always uses UTF-8 encoding and this * method does not rely on JVM default since there is no standard way to enforce it. JRE specification states that * <code>file.encoding</code> is not the standard way to set default charset and to use host settings. * * @param stream input stream. * @return newly created buffered reader. */ public static BufferedReader createBufferedReader(InputStream stream) { try { return new BufferedReader(new InputStreamReader(stream, "UTF-8")); } catch(UnsupportedEncodingException unused) { throw new BugError("Unsupported UTF-8 ecoding."); } }
/** * Get object instance field value. Reflective field argument should have accessibility set to true and this condition * is fulfilled if field is obtained via {@link #getField(Class, String)}. * * @param object instance to retrieve field value from, * @param field object reflective field. * @param <T> field type. * @return field value, possible null. */ @SuppressWarnings("unchecked") public static <T> T getFieldValue(Object object, Field field) { assert field.isAccessible(); try { return (T)field.get(object); } catch(IllegalAccessException e) { throw new BugError(e); } }
/** * Construct single locale repository for requested repository directory and file filter. * * @param repositoryDir repository directory, * @param fileFilter files filter, possibly accepting all. * @throws IOException if listing repository files fails. * @throws BugError if no files matching files filter found. */ public SingleLocale(File repositoryDir, FileFilter fileFilter) throws IOException { files = repositoryDir.listFiles(fileFilter); if(files == null) { throw new IOException(String.format("Listing files from repository directory |%s| fails.", repositoryDir)); } if(files == null || files.length == 0) { throw new BugError("No files |%s| found in repository directory |%s|.", fileFilter, repositoryDir); } }
@Override public Document parseXMLNS(String string) { try { return loadXMLNS(new ByteArrayInputStream(string.getBytes("UTF-8"))); } catch (UnsupportedEncodingException e) { throw new BugError("JVM with missing support for UTF-8."); } }
/** * Get boxing class for requested type. If <code>type</code> is primitive returns related boxing class. If * <code>type</code> is already a boxing type returns it as it is. It is considered a bug if <code>type</code> is not * a primitive or a boxing type. * * @param t primitive or boxing type. * @return boxing class representing requested type. * @throws BugError if <code>type</code> is not a primitive or boxing type. */ public static Class<?> getBoxingClass(Type t) throws BugError { Type boxingClass = BOXING_MAP.get(t); if(boxingClass == null) { if(!BOXING_MAP.values().contains(t)) { throw new BugError("Trying to get boxing class from not boxed type."); } boxingClass = t; } return (Class<?>)boxingClass; }
@Override public ManagedMethodSPI getManagedMethod(Method method) throws NoSuchMethodException { if (!instanceType.equals(InstanceType.PROXY)) { throw new BugError("Managed method getter can be used only on |%s| types.", InstanceType.PROXY); } ManagedMethodSPI managedMethod = methodsPool.get(method); if (managedMethod == null) { throw new NoSuchMethodException(String.format("Missing managed method |%s#%s|.", implementationClass.getName(), method.getName())); } return managedMethod; }
/** * Encode string value into Base64 format. * * @param string string to encode. * @return given <code>string</code> value encoded Base64. */ public static String encode(String string) { try { return encode(string.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new BugError("JVM with missing support for UTF-8."); } }
@Override public Document parseXML(String string) { try { return loadXML(new ByteArrayInputStream(string.getBytes("UTF-8"))); } catch (UnsupportedEncodingException e) { throw new BugError("JVM with missing support for UTF-8."); } }
/** * Initialize iterators for managed classes and current class methods. */ public ManagedMethodsIterator() { classesIterator = classesPool.values().iterator(); if (!classesIterator.hasNext()) { throw new BugError("Empty classes pool."); } currentMethodsIterator = classesIterator.next().getManagedMethods().iterator(); }
/** * Register instance factory to requested instance type. * * @param instanceType instance type used as key for instance factory, * @param instanceFactory instance factory. * @throws BugError if instance type is already registered. */ protected void registerInstanceFactory(InstanceType instanceType, InstanceFactory instanceFactory) { log.debug("Register instance factory |%s| to |%s|.", instanceFactory.getClass(), instanceType); if (instanceFactories.put(instanceType, instanceFactory) != null) { throw new BugError("Attempt to override instance type |%s|.", instanceType); } }