use of org.osgl.exception.UnexpectedException in project actframework by actframework.
the class Act method startup.
public static void startup(AppDescriptor descriptor) {
processEnvironment(descriptor);
Banner.print(descriptor);
loadConfig();
initMetricPlugin();
initPluginManager();
initAppServicePluginManager();
initDbManager();
initEnhancerManager();
initViewManager();
initAppCodeScannerPluginManager();
loadPlugins();
// so it can order the enhancers
enhancerManager().registered();
initNetworkLayer();
initApplicationManager();
LOGGER.info("loading application(s) ...");
appManager.loadSingleApp(descriptor);
startNetworkLayer();
Thread.currentThread().setContextClassLoader(Act.class.getClassLoader());
App app = app();
if (null == app) {
shutdownNetworkLayer();
throw new UnexpectedException("App not found. Please make sure your app start directory is correct");
}
emit(SysEventId.ACT_START);
writePidFile();
}
use of org.osgl.exception.UnexpectedException in project actframework by actframework.
the class JsonDTOClassManager method extractBeanSpec.
private void extractBeanSpec(List<BeanSpec> beanSpecs, Method method, Class host) {
Type[] paramTypes = method.getGenericParameterTypes();
int sz = paramTypes.length;
if (0 == sz) {
return;
}
Annotation[][] annotations = method.getParameterAnnotations();
for (int i = 0; i < sz; ++i) {
Type type = paramTypes[i];
if (type instanceof TypeVariable && !Modifier.isStatic(method.getModifiers())) {
// explore type variable impl
TypeVariable typeVar = $.cast(type);
String typeVarName = typeVar.getName();
// find all generic types on host
Map<String, Class> typeVarLookup = Generics.buildTypeParamImplLookup(host);
type = typeVarLookup.get(typeVarName);
if (null == type) {
throw new UnexpectedException("Cannot determine concrete type of method parameter %s", typeVarName);
}
}
Annotation[] anno = annotations[i];
BeanSpec spec = BeanSpec.of(type, anno, injector);
if (ParamValueLoaderService.providedButNotDbBind(spec, injector)) {
continue;
}
String dbBindName = dbBindName(spec);
if (null != dbBindName) {
beanSpecs.add(BeanSpec.of(String.class, new Annotation[0], dbBindName, injector));
} else {
beanSpecs.add(spec);
}
}
}
use of org.osgl.exception.UnexpectedException in project actframework by actframework.
the class ResourceLoader method _load.
protected static Object _load(String resourcePath, BeanSpec spec, boolean ignoreResourceNotFound) {
URL url = loadResource(resourcePath);
if (null == url) {
if (!ignoreResourceNotFound) {
LOGGER.warn("resource not found: " + resourcePath);
}
return null;
}
boolean isJson = resourcePath.endsWith(".json");
Class<?> rawType = spec.rawType();
if (URL.class == rawType) {
return url;
} else if (String.class == rawType) {
return IO.readContentAsString(url);
} else if (byte[].class == rawType) {
return readContent(url);
} else if (List.class.equals(rawType)) {
List<Type> typeParams = spec.typeParams();
if (!typeParams.isEmpty()) {
if (String.class == typeParams.get(0)) {
return IO.readLines(url);
} else if (isJson) {
return JSON.parseObject(IO.readContentAsString(url), spec.type());
}
}
} else if (Collection.class.isAssignableFrom(rawType)) {
List<Type> typeParams = spec.typeParams();
if (!typeParams.isEmpty()) {
Collection col = (Collection) Act.getInstance(rawType);
if (String.class == typeParams.get(0)) {
col.addAll(IO.readLines(url));
return col;
} else {
StringValueResolverManager resolverManager = Act.app().resolverManager();
try {
Class componentType = spec.componentSpec().rawType();
List<String> stringList = IO.readLines(url);
for (String line : stringList) {
col.add(resolverManager.resolve(line, componentType));
}
} catch (RuntimeException e) {
throw new UnexpectedException("return type not supported: " + spec);
}
}
}
} else if (ByteBuffer.class == rawType) {
byte[] ba = readContent(url);
ByteBuffer buffer = ByteBuffer.allocateDirect(ba.length);
buffer.put(ba);
buffer.flip();
return buffer;
} else if (Path.class.isAssignableFrom(rawType)) {
try {
return Paths.get(url.toURI());
} catch (URISyntaxException exception) {
throw E.unexpected(exception);
}
} else if (File.class.isAssignableFrom(rawType)) {
return new File(url.getFile());
} else if (ISObject.class.isAssignableFrom(rawType)) {
return SObject.of(readContent(url));
} else if (InputStream.class == rawType) {
return IO.is(url);
} else if (Reader.class == rawType) {
return new InputStreamReader(IO.is(url));
}
if (isJson) {
return JSON.parseObject(IO.readContentAsString(url), spec.type());
}
try {
return Act.app().resolverManager().resolve(IO.readContentAsString(url), rawType);
} catch (RuntimeException e) {
throw new UnexpectedException("return type not supported: " + spec);
}
}
use of org.osgl.exception.UnexpectedException in project actframework by actframework.
the class ApacheMultipartParser method parse.
@Override
public Map<String, String[]> parse(ActionContext context) {
H.Request request = context.req();
InputStream body = request.inputStream();
Map<String, String[]> result = new HashMap<>();
try {
FileItemIteratorImpl iter = new FileItemIteratorImpl(body, request.header("content-type"), request.characterEncoding());
while (iter.hasNext()) {
FileItemStream item = iter.next();
ISObject sobj = UploadFileStorageService.store(item, context.app());
if (sobj.getLength() == 0L) {
continue;
}
String fieldName = item.getFieldName();
if (item.isFormField()) {
// must resolve encoding
// this is our default
String _encoding = request.characterEncoding();
String _contentType = item.getContentType();
if (_contentType != null) {
ContentTypeWithEncoding contentTypeEncoding = ContentTypeWithEncoding.parse(_contentType);
if (contentTypeEncoding.encoding != null) {
_encoding = contentTypeEncoding.encoding;
}
}
mergeValueInMap(result, fieldName, sobj.asString(Charset.forName(_encoding)));
} else {
context.addUpload(item.getFieldName(), sobj);
mergeValueInMap(result, fieldName, fieldName);
}
}
} catch (FileUploadIOException e) {
throw E.ioException("Error when handling upload", e);
} catch (IOException e) {
throw E.ioException("Error when handling upload", e);
} catch (FileUploadException e) {
throw E.ioException("Error when handling upload", e);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new UnexpectedException(e);
}
return result;
}
use of org.osgl.exception.UnexpectedException in project actframework by actframework.
the class TextParser method parse.
@Override
public Map<String, String[]> parse(ActionContext context) {
H.Request req = context.req();
InputStream is = req.inputStream();
try {
Map<String, String[]> params = new HashMap<String, String[]>();
ByteArrayOutputStream os = new ByteArrayOutputStream();
int b;
while ((b = is.read()) != -1) {
os.write(b);
}
byte[] data = os.toByteArray();
params.put(ActionContext.REQ_BODY, data.length == 0 ? null : new String[] { new String(data, req.characterEncoding()) });
return params;
} catch (Exception e) {
throw new UnexpectedException(e);
}
}
Aggregations