/** * 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; } });
@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."); } }
@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); } }
/** * Serialize event as JSON stream to given writer. Since writer is a {@link PrintWriter} it does not throw * {@link IOException}. As a consequence this method does not throw exception too. * * @param writer print writer, * @param event event object. */ public void serialize(PrintWriter writer, Event event) { try { json.stringifyObject(writer, event); } catch (IOException e) { // print writer never throws IO exceptions throw new BugError(e); } }
/** * Register instance processor. Only a single instance per processor class is allowed. * * @param instanceProcessor instance processor. * @throws BugError if instance processor class is already registered. */ protected void registerInstanceProcessor(InstanceProcessor instanceProcessor) { for (InstanceProcessor existingInstanceProcessoor : instanceProcessors) { if (existingInstanceProcessoor.getClass().equals(instanceProcessor.getClass())) { throw new BugError("Attempt to override instance processor |%s|.", instanceProcessor.getClass()); } } log.debug("Register instance processor |%s|.", instanceProcessor.getClass()); instanceProcessors.add(instanceProcessor); }
@Override public void dump() { try { Serializer serializer = new Serializer(new OutputStreamWriter(System.out, "UTF-8")); serializer.serialize(this); } catch (Exception e) { // hard to believe standard out will fail to write throw new BugError(e); } }
@Override public <T> T getRemoteInstance(String implementationURL, Class<? super T> interfaceClass) { Params.notNull(implementationURL, "Implementation URL"); Params.notNull(interfaceClass, "Interface class"); Params.isTrue(interfaceClass.isInterface(), "Interface class argument is not actually an interface."); InstanceFactory instanceFactory = instanceFactories.get(InstanceType.REMOTE); if (instanceFactory == null) { throw new BugError("No REMOTE instance factory registered."); } if (!(instanceFactory instanceof RemoteFactory)) { throw new BugError("Invalid REMOTE instance factory provider. Does not implement %s interface.", RemoteFactory.class); } return ((RemoteFactory) instanceFactory).getRemoteInstance(implementationURL, interfaceClass); }
/** * Construct transactional resource for application served by given factory. * * @param appFactory application factory. */ public TransactionalResourceImpl(AppFactory appFactory) { log.trace("TransactionalResource(AppFactory)"); // uses AppFactory instead of Classes.loadService to acquire transaction manager since need scope management this.transactionManager = appFactory.getOptionalInstance(TransactionManager.class); if (this.transactionManager == null) { throw new BugError("Transaction manager service not found. Ensure <data-source> is configured on application descriptor and there is a service provider on run-time."); } }
/** * Get HTTP request from current request context. * * @return current HTTP request. * @throws BugError if attempt to use not initialized HTTP request. */ private HttpServletRequest getHttpServletRequest() { RequestContext context = getInstance(RequestContext.class); HttpServletRequest request = context.getRequest(); if (request == null) { throw new BugError("Attempt to use not initialized HTTP request."); } return request; } }
@Override public <T> T getInstance(String instanceName, Class<? super T> interfaceClass, Object... args) { Params.notNullOrEmpty(instanceName, "Instance name"); Params.notNull(interfaceClass, "Interface class"); ManagedClassSPI managedClass = classesPool.get(interfaceClass); if (managedClass == null) { throw new BugError("No managed class associated with interface class |%s|.", interfaceClass); } // instance name should be unique and can be used as uniqueness indicator InstanceKey key = new InstanceKey(instanceName); return getInstance(managedClass, key, args); }
@Override public <T> T getInstance(Class<? super T> interfaceClass, Object... args) { Params.notNull(interfaceClass, "Interface class"); ManagedClassSPI managedClass = classesPool.get(interfaceClass); if (managedClass == null) { throw new BugError("No managed class associated with interface class |%s|.", interfaceClass); } InstanceKey instanceKey = new InstanceKey(managedClass.getKey()); return getInstance(managedClass, instanceKey, args); }
@Override public EList call(String elementMethodName, Object... args) { for (int i = 0; i < size(); ++i) { Element element = item(i); try { Classes.invoke(element, elementMethodName, args); } catch (Throwable e) { throw new BugError(e); } } return this; }