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

The following examples show how to use org.rapidoid.u.U#rte() . 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: RapidoidConnection.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
void processedSeq(long processedHandle) {

		if (processedHandle == 0) return; // a new connection

		U.must(processedHandle > 0);

		boolean increased = writeSeq.compareAndSet(processedHandle - 1, processedHandle);

		if (!increased) {
			// the current response might be already marked as processed (e.g. in non-async handlers)
			long writeSeqN = writeSeq.get();
			if (writeSeqN != processedHandle) {
				throw U.rte("Error in the response order control! Expected handle: %s, real: %s", processedHandle - 1, writeSeqN);
			}
		}
	}
 
Example 2
Source File: Msc.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
public static Map<String, String> parseArgs(List<String> args) {
	Map<String, String> arguments = U.map();

	for (String arg : U.safe(args)) {
		if (!isSpecialArg(arg)) {

			String[] parts = arg.split("=", 2);
			U.must(parts.length == 2, "The argument '%s' doesn't have a key=value format!", arg);

			arguments.put(parts[0], parts[1]);

		} else {
			throw U.rte("Special arguments are not supported since v6.0. Found: " + arg);
		}
	}

	return arguments;
}
 
Example 3
Source File: Cls.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
public static List<Method> getMethodsNamed(Class<?> clazz, String name) {
	List<Method> methods = U.list();

	try {
		for (Class<?> c = clazz; c.getSuperclass() != null; c = c.getSuperclass()) {
			for (Method method : c.getDeclaredMethods()) {
				if (method.getName().equals(name)) {
					methods.add(method);
				}
			}
		}

	} catch (Exception e) {
		throw U.rte("Cannot instantiate class!", e);
	}

	return methods;
}
 
Example 4
Source File: JPAUtil.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
private static EntityManagerFactory createEMF(Properties props, CustomHibernatePersistenceProvider provider) {
	while (true) {
		try {
			EntityManagerFactory emf = provider.createEMF(props);
			U.must(emf != null, "Failed to initialize EntityManagerFactory with Hibernate!");
			return emf;

		} catch (Exception e) {
			if (Msc.rootCause(e) instanceof ConnectException) {
				Log.warn("Couldn't connect, will retry again in 3 seconds...");
				U.sleep(3000); // FIXME improve back-off
			} else {
				throw U.rte("Failed to create EMF!", e);
			}
		}
	}
}
 
Example 5
Source File: BeanProp.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> T getRaw(Object target) {
	// FIXME when target class isn't the property declaring class

	try {
		if (getter != null) {
			return (T) getter.invoke(target);
		} else {
			return (T) field.get(target);
		}

	} catch (Exception e) {
		if (Msc.rootCause(e) instanceof UnsupportedOperationException) {
			return null;
		} else {
			throw U.rte(e);
		}
	}
}
 
Example 6
Source File: RapidoidClientLoop.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized ChannelHolder connect(String serverHost, int serverPort, Protocol clientProtocol,
                                          boolean autoreconnecting, ConnState state) {

	InetSocketAddress addr = new InetSocketAddress(serverHost, serverPort);
	SocketChannel socketChannel = openSocket();

	ChannelHolderImpl holder = new ChannelHolderImpl();

	try {
		ExtendedWorker targetWorker = ioWorkers[currentWorkerInd];
		ConnectionTarget target = new ConnectionTarget(socketChannel, addr, clientProtocol, holder, autoreconnecting, state);
		targetWorker.connect(target);

	} catch (IOException e) {
		throw U.rte("Cannot create a TCP client connection!", e);
	}

	switchToNextWorker();
	return holder;
}
 
Example 7
Source File: MultiBuf.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
private void validatePos(int pos, int space) {
	if (pos < 0) {
		throw U.rte("Invalid position: " + pos);
	}

	int least = pos + space;

	boolean hasEnough = least <= _size() && least <= _limit;

	if (!hasEnough) {
		throw INCOMPLETE_READ;
	}
}
 
Example 8
Source File: AESCypherTool.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
private static int calcAESKeyLength() {
	int maxKeyLen;

	try {
		maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
	} catch (NoSuchAlgorithmException e) {
		throw U.rte(e);
	}

	return maxKeyLen > 256 ? 256 : 128;
}
 
Example 9
Source File: RapidoidConnection.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized Channel write(File file) {
	try {
		FileInputStream stream = new FileInputStream(file);
		FileChannel fileChannel = stream.getChannel();
		output.append(fileChannel);
		stream.close();
	} catch (IOException e) {
		throw U.rte(e);
	}

	return this;
}
 
Example 10
Source File: ClasspathUtil.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public static Enumeration<URL> getResources(String name) {
	name = name.replace('.', '/');
	try {
		return Cls.classLoader().getResources(name);
	} catch (IOException e) {
		throw U.rte("Cannot scan: " + name, e);
	}
}
 
Example 11
Source File: Cls.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public static Class<?>[] getImplementedInterfaces(Class<?> clazz) {
	try {
		List<Class<?>> interfaces = new LinkedList<>();

		for (Class<?> c = clazz; c.getSuperclass() != null; c = c.getSuperclass()) {
			for (Class<?> interf : c.getInterfaces()) {
				interfaces.add(interf);
			}
		}

		return interfaces.toArray(new Class<?>[interfaces.size()]);
	} catch (Exception e) {
		throw U.rte("Cannot retrieve implemented interfaces!", e);
	}
}
 
Example 12
Source File: Opt.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public E orFail(String errMsg) {
	if (exists()) {
		return get();
	} else {
		throw U.rte(errMsg);
	}
}
 
Example 13
Source File: Dates.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public static Date date(String value) {
	if (U.isEmpty(value)) {
		return null;
	}

	String[] parts = value.split("(\\.|-|/)");

	int a = parts.length > 0 ? U.num(parts[0]) : -1;
	int b = parts.length > 1 ? U.num(parts[1]) : -1;
	int c = parts.length > 2 ? U.num(parts[2]) : -1;

	switch (parts.length) {
		case 3:
			if (isDay(a) && isMonth(b) && isYear(c)) {
				return date(a, b, c);
			} else if (isYear(a) && isMonth(b) && isDay(c)) {
				return date(c, b, a);
			}
			break;
		case 2:
			if (isDay(a) && isMonth(b)) {
				return date(a, b, thisYear());
			}
			break;
		default:
	}

	throw U.rte("Invalid date: " + value);
}
 
Example 14
Source File: FiniteStateProtocol.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
protected int state12(Channel ctx) {
	throw U.rte("State 12 handler is not implemented!");
}
 
Example 15
Source File: MapProp.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public Class<?> getRawTypeArg(int index) {
	throw U.rte("No type args available!");
}
 
Example 16
Source File: FiniteStateProtocol.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
protected int state9(Channel ctx) {
	throw U.rte("State 9 handler is not implemented!");
}
 
Example 17
Source File: FiniteStateProtocol.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
protected int state6(Channel ctx) {
	throw U.rte("State 6 handler is not implemented!");
}
 
Example 18
Source File: FiniteStateProtocol.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
protected int state7(Channel ctx) {
	throw U.rte("State 7 handler is not implemented!");
}
 
Example 19
Source File: FiniteStateProtocol.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
protected int state19(Channel ctx) {
	throw U.rte("State 19 handler is not implemented!");
}
 
Example 20
Source File: FiniteStateProtocol.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
protected int state15(Channel ctx) {
	throw U.rte("State 15 handler is not implemented!");
}