Java Code Examples for org.rapidoid.u.U#notNull()

The following examples show how to use org.rapidoid.u.U#notNull() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpringIntegrator.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void postConstructInitialization() {
	U.notNull(ctx, "ConfigurableApplicationContext ctx");

	if (useProfiles()) {
		initProfiles();
	}

	if (useEmf() && JPA.getEmf() == null && MscOpts.hasJPA()) {
		initJPA();
	}

	if (useBeans()) {
		initBeans();
	}

	run();
}
 
Example 2
Source File: TLSUtil.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
private static TrustManager[] initTrustManagers(String trustStoreFilename, char[] trustStorePassword) throws Exception {
	if (U.notEmpty(trustStoreFilename)) {

		U.notNull(trustStorePassword, "trustStorePassword");

		KeyStore trustStore = KeyStore.getInstance("JKS");
		trustStore.load(new FileInputStream(trustStoreFilename), trustStorePassword);

		TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
		trustManagerFactory.init(trustStore);

		return trustManagerFactory.getTrustManagers();

	} else {
		return null;
	}
}
 
Example 3
Source File: RespImpl.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
public void chunk(final byte[] data, final int offset, final int length) {
	U.notNull(data, "data");

	resume(() -> {
		Buf out = req.channel().output();

		out.append(Integer.toHexString(length));
		out.append("\r\n");
		out.append(data, offset, length);
		out.append("\r\n");

		req.channel().send();

		return false;
	});
}
 
Example 4
Source File: ReqImpl.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
private void saveSession(Map<String, Serializable> session) {
	SessionManager sessionManager = U.notNull(custom().sessionManager(), "session manager");

	try {
		sessionManager.saveSession(this, sessionId(), session);
	} catch (Exception e) {
		throw U.rte("Error occurred while saving the session!", e);
	}
}
 
Example 5
Source File: ClassReloader.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public Class<?> loadClass(String classname) throws ClassNotFoundException {
	U.notNull(classname, "classname");

	Log.debug("Loading class", "name", classname);

	Class<?> cls = findLoadedClass(classname);

	if (cls != null) {
		return cls;
	}

	if (shouldReload(classname)) {

		try {
			Class<?> reloaded = findClass(classname);

			return !reloadVeto(reloaded) ? reloaded : super.loadClass(classname);

		} catch (ClassNotFoundException e) {
			Class<?> fallbackClass = super.loadClass(classname);
			Log.debug("Couldn't reload class, fallback load", "name", classname);
			return fallbackClass;
		}

	} else {
		return super.loadClass(classname);
	}
}
 
Example 6
Source File: Secure.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public static DataPermissions getObjectPermissions(String username, Set<String> roles, Object target) {
	U.notNull(target, "target");
	Class<?> clazz = target.getClass();
	clazz = Cls.unproxy(clazz);

	if (Collection.class.isAssignableFrom(clazz) || Map.class.isAssignableFrom(clazz)
		|| Object[].class.isAssignableFrom(clazz)) {
		return DataPermissions.ALL;
	}

	if (!hasRoleBasedAccess(username, roles, clazz, null)) {
		return DataPermissions.NONE;
	}

	CanRead canRead = Metadata.classAnnotation(clazz, CanRead.class);
	CanInsert canInsert = Metadata.classAnnotation(clazz, CanInsert.class);
	CanChange canChange = Metadata.classAnnotation(clazz, CanChange.class);
	CanDelete canDelete = Metadata.classAnnotation(clazz, CanDelete.class);
	CanManage canManage = Metadata.classAnnotation(clazz, CanManage.class);

	if (canRead == null && canInsert == null && canChange == null && canDelete == null && canManage == null) {
		return DataPermissions.ALL;
	}

	boolean read = canRead == null || hasAnyRole(username, roles, roles(canRead.value()), clazz, target);

	boolean insert = canInsert != null && hasAnyRole(username, roles, roles(canInsert.value()), clazz, target);
	boolean change = canChange != null && hasAnyRole(username, roles, roles(canChange.value()), clazz, target);
	boolean delete = canDelete != null && hasAnyRole(username, roles, roles(canDelete.value()), clazz, target);

	boolean manage = canManage != null && hasAnyRole(username, roles, roles(canManage.value()), clazz, target);
	insert |= manage;
	change |= manage;
	delete |= manage;

	return DataPermissions.from(read, insert, change, delete);
}
 
Example 7
Source File: WatcherThread.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
private void init(Path dir) {
	Log.debug("Registering directory watch", "dir", dir);

	try {
		WatchKey key = dir.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
		U.notNull(key, "watch key");

		keys.put(key, dir);

	} catch (IOException e) {
		Log.warn("Couldn't register to watch for changes", "dir", dir);
	}
}
 
Example 8
Source File: GroupOf.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public E find(String id) {
	U.notNull(id, "id");

	for (E item : items) {
		if (U.eq(id, item.id())) return item;
	}

	return null;
}
 
Example 9
Source File: JPAUtil.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public static EntityManager em() {
	Ctx ctx = Ctxs.get();

	if (ctx != null) {
		return JPAInternals.wrapEM(ctx.persister());

	} else {
		EntityManagerFactory emf = JPAUtil.emf;
		U.notNull(emf, "JPA.emf");
		return JPAInternals.wrapEM(emf.createEntityManager());
	}
}
 
Example 10
Source File: ReqImpl.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public String cookie(String name) {
	return U.notNull(cookies().get(name), "COOKIES[%s]", name);
}
 
Example 11
Source File: AbstractValue.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public Value<T> orElse(Value<T> alternative) {
	U.notNull(alternative, "alternative");
	return new OrValue<>(this, alternative);
}
 
Example 12
Source File: EnvProperties.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public Object get(String key) {
	U.notNull(key, "key");

	return props.get(normalize(key));
}
 
Example 13
Source File: Environment.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, String> call() {
	U.notNull(args, "environment args");
	return Msc.parseArgs(args);
}
 
Example 14
Source File: ReqImpl.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public String header(String name) {
	return U.notNull(headers().get(name.toLowerCase()), "HEADERS[%s]", name);
}
 
Example 15
Source File: IoCContextImpl.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
private <T> T provideBindValue(Object target, Class<T> type, String name, Mapper<String, Object> bindings) {
	U.notNull(bindings, "bindings");
	Object value = Lmbd.eval(bindings, name);
	return value != null ? Cls.convert(value, type) : null;
}
 
Example 16
Source File: HttpRoutesImpl.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
private void addOrRemove(boolean add, String verbs, String path, HttpHandler handler) {
	U.notNull(verbs, "HTTP verbs");
	U.notNull(path, "HTTP path");

	U.must(path.startsWith("/"), "The URI must start with '/', but found: '%s'", path);

	initialize();

	if (add) {
		U.notNull(handler, "HTTP handler");
	}

	verbs = verbs.toUpperCase();
	if (path.length() > 1) {
		path = Str.trimr(path, "/");
	}

	if (add) {
		RouteOptions opts = handler.options();

		TransactionMode txm = opts.transaction();
		String tx = txm != TransactionMode.NONE ? AnsiColor.bold(txm.name()) : txm.name();

		int space = Math.max(45 - verbs.length() - path.length(), 1);
		Log.info(httpVerbColor(verbs) + AnsiColor.bold(" " + path) + Str.mul(" ", space), "setup", setupName,
			"!roles", opts.roles(), "transaction", tx, "mvc", opts.mvc(), "cacheTTL", opts.cacheTTL());

	} else {
		Log.info("Deregistering handler", "setup", setupName, "!verbs", verbs, "!path", path);
	}

	for (String vrb : verbs.split(",")) {
		HttpVerb verb = HttpVerb.from(vrb);

		if (add) {
			deregister(verb, path);
			register(verb, path, handler);
		} else {
			deregister(verb, path);
		}
	}

	notifyChanged();
}
 
Example 17
Source File: ReqImpl.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T extends Serializable> T posted(String name) {
	return (T) U.notNull(posted().get(name), "POSTED[%s]", name);
}
 
Example 18
Source File: RapidoidModules.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
private static void validate(List<RapidoidModule> modules) {
	for (RapidoidModule module : modules) {
		U.notNull(module.name(), "the name of module %s", module.getClass().getSimpleName());
	}
}
 
Example 19
Source File: Secure.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public static boolean hasRoleBasedMethodAccess(String username, Set<String> roles, Method method) {
	U.notNull(method, "method");
	Set<String> rolesAllowed = getRolesAllowed(method);
	return U.isEmpty(rolesAllowed) || hasAnyRole(username, roles, rolesAllowed);
}
 
Example 20
Source File: RapidoidServerLoop.java    From rapidoid with Apache License 2.0 3 votes vote down vote up
private void openSocket() throws IOException {
	U.notNull(net.protocol(), "protocol");
	U.notNull(net.helperClass(), "helperClass");

	String blockingInfo = net.blockingAccept() ? "blocking" : "non-blocking";
	Log.debug("Initializing server", "address", net.address(), "port", net.port(), "sync", net.syncBufs(), "accept", blockingInfo);

	serverSocketChannel = ServerSocketChannel.open();

	if ((serverSocketChannel.isOpen()) && (selector.isOpen())) {

		serverSocketChannel.configureBlocking(net.blockingAccept());

		ServerSocket socket = serverSocketChannel.socket();

		Log.info("!Starting server", "!address", net.address(), "!port", net.port(), "I/O workers", net.workers(), "sync", net.syncBufs(), "accept", blockingInfo);

		InetSocketAddress addr = new InetSocketAddress(net.address(), net.port());

		socket.setReceiveBufferSize(16 * 1024);
		socket.setReuseAddress(true);
		socket.bind(addr, MAX_PENDING_CONNECTIONS);

		Log.debug("Opened server socket", "address", addr);

		if (!net.blockingAccept()) {
			Log.debug("Registering accept selector");
			serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
		}

		initWorkers();

	} else {
		throw U.rte("Cannot open socket!");
	}
}