Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion code/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>22.0.0</version>
<version>24.0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
5 changes: 5 additions & 0 deletions code/src/main/java/org/nocturne/caption/CaptionsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package org.nocturne.caption;

import com.google.inject.Singleton;
import org.apache.log4j.Logger;
import org.nocturne.exception.ConfigurationException;
import org.nocturne.main.ApplicationContext;

Expand Down Expand Up @@ -31,6 +32,8 @@
*/
@Singleton
public class CaptionsImpl implements Captions {
private static final Logger logger = Logger.getLogger(CaptionsImpl.class);

private static final Pattern CAPTIONS_FILE_PATTERN = Pattern.compile("captions_[\\w]{2}\\.properties");

/**
Expand Down Expand Up @@ -135,6 +138,7 @@ private static void save(Properties properties, String language) {
properties.store(writer, null);
writer.close();
} catch (IOException e) {
logger.error("Can't write into file " + file + '.', e);
throw new ConfigurationException("Can't write into file " + file + '.', e);
}
}
Expand Down Expand Up @@ -166,6 +170,7 @@ private void loadPropertiesForProduction() {
reader.close();
propertiesMap.put(language, properties);
} catch (IOException e) {
logger.error("Can't load caption properties for language " + language + '.', e);
throw new ConfigurationException("Can't load caption properties for language " + language + '.', e);
}
}
Expand Down
39 changes: 32 additions & 7 deletions code/src/main/java/org/nocturne/link/Links.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
* @author Mike Mirzayanov
*/
public class Links {
private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Links.class);

private static final Lock addLinkLock = new ReentrantLock();

private static final int INTERCEPTOR_MAX_PERMIT_COUNT = 8 * Runtime.getRuntime().availableProcessors();
Expand Down Expand Up @@ -107,6 +109,8 @@ public static String getLinkName(@Nonnull Class<? extends Page> pageClass) {
}

if (clazz == null) {
logger.error("Page class should have @Link or @LinkSet annotation, but "
+ pageClass.getName() + " hasn't.");
throw new NocturneException("Page class should have @Link or @LinkSet annotation, but "
+ pageClass.getName() + " hasn't.");
}
Expand All @@ -131,6 +135,7 @@ public static void add(Class<? extends Page> clazz, List<Link> linkSet) {
try {
String name = getLinkName(clazz);
if (classesByName.containsKey(name) && !clazz.equals(classesByName.get(name))) {
logger.error("Can't add page which is not unique by it's name: " + clazz.getName() + '.');
throw new ConfigurationException("Can't add page which is not unique by it's name: "
+ clazz.getName() + '.');
}
Expand All @@ -151,10 +156,12 @@ public static void add(Class<? extends Page> clazz, List<Link> linkSet) {

for (Map<String, Link> linkMap : linksByPage.values()) {
if (linkMap.containsKey(pageLink)) {
logger.error("Page link \"" + pageLink + "\" already registered.");
throw new ConfigurationException("Page link \"" + pageLink + "\" already registered.");
}
}
if (links.containsKey(pageLink)) {
logger.error("Page link \"" + pageLink + "\" already registered.");
throw new ConfigurationException("Page link \"" + pageLink + "\" already registered.");
}

Expand All @@ -176,6 +183,7 @@ public static void add(Class<? extends Page> clazz, List<Link> linkSet) {
public static void add(Class<? extends Page> clazz) {
List<Link> linkSet = getLinksViaReflection(clazz);
if (linkSet.isEmpty()) {
logger.error("Can't find link for page " + clazz.getName() + '.');
throw new ConfigurationException("Can't find link for page " + clazz.getName() + '.');
}

Expand Down Expand Up @@ -227,11 +235,12 @@ public static String getLinkByMap(Class<? extends Page> clazz, @Nullable String

if (bestMatchedLinkSections == null) {
if (linkName == null || linkName.isEmpty()) {
logger.error("Can't find link for page " + clazz.getName() + '.');
throw new NoSuchLinkException("Can't find link for page " + clazz.getName() + '.');
} else {
throw new NoSuchLinkException(
"Can't find link with name \'" + linkName + "\' for page " + clazz.getName() + '.'
);
logger.error("Can't find link with name '" + linkName + "' for page " + clazz.getName() + '.');
throw new NoSuchLinkException("Can't find link with name '"
+ linkName + "' for page " + clazz.getName() + '.');
}
}

Expand Down Expand Up @@ -292,6 +301,7 @@ public static String getLinkByMap(Class<? extends Page> clazz, @Nullable String
for (String skipInterceptor : bestMatchedLink.skipInterceptors()) {
if (skipInterceptor.equals(e.getKey())) {
skip = true;
break;
}
}
if (!skip) {
Expand Down Expand Up @@ -360,6 +370,7 @@ private static List<String> toStringList(@Nonnull TemplateSequenceModel sequence
try {
item = sequence.get(i);
} catch (TemplateModelException e) {
logger.error("Can't get item of Freemarker sequence.", e);
throw new NocturneException("Can't get item of Freemarker sequence.", e);
}

Expand Down Expand Up @@ -387,6 +398,7 @@ private static int getSize(@Nonnull TemplateSequenceModel sequence) {
try {
return sequence.size();
} catch (TemplateModelException e) {
logger.error("Can't get size of Freemarker sequence.", e);
throw new NocturneException("Can't get size of Freemarker sequence.", e);
}
}
Expand Down Expand Up @@ -416,6 +428,7 @@ public static String getLinkByMap(String name, @Nullable String linkName, Map<St
Class<? extends Page> clazz = classesByName.get(name);

if (clazz == null) {
logger.error("Can't find link for page " + name + '.');
throw new NoSuchLinkException("Can't find link for page " + name + '.');
} else {
return getLinkByMap(clazz, linkName, params);
Expand Down Expand Up @@ -462,6 +475,7 @@ private static Map<String, Object> convertArrayToMap(Object... params) {
}

if (paramCount % 2 != 0) {
logger.error("Params should contain even number of elements.");
throw new IllegalArgumentException("Params should contain even number of elements.");
}

Expand All @@ -480,7 +494,7 @@ private static Map<String, Object> convertArrayToMap(Object... params) {
* @throws NoSuchLinkException if no such link exists
*/
public static String getLink(Class<? extends Page> pageClass) {
return getLinkByMap(pageClass, null, Collections.<String, Object>emptyMap());
return getLinkByMap(pageClass, null, Collections.emptyMap());
}

/**
Expand Down Expand Up @@ -524,6 +538,7 @@ public static LinkMatchResult match(String link) {
}

if (!link.startsWith("/")) {
logger.error("Link \"" + link + "\" doesn't start with '/'.");
throw new IllegalArgumentException("Link \"" + link + "\" doesn't start with '/'.");
}

Expand Down Expand Up @@ -559,6 +574,7 @@ public static LinkMatchResult match(String link) {
private static Map<String, String> match(String[] linkTokens, String linkText) {
List<LinkSection> sections = sectionsByLinkText.get(linkText);
if (sections == null) {
logger.error("Can't find sections for linkText=\"" + linkText + "\".");
throw new NocturneException("Can't find sections for linkText=\"" + linkText + "\".");
}

Expand Down Expand Up @@ -603,6 +619,8 @@ public NoSuchLinkException(String message) {

private static List<LinkSection> parseLinkToLinkSections(String linkText) {
if (linkText == null || linkText.startsWith("/") || linkText.endsWith("/")) {
logger.error("Page link has illegal format, use links like 'home', 'page/{index}', " +
"'page/{index(long,positive):1,2,3}', 'section/{name(string,!blank):!a,b,c}'.");
throw new ConfigurationException("Page link has illegal format, use links like 'home', 'page/{index}', " +
"'page/{index(long,positive):1,2,3}', 'section/{name(string,!blank):!a,b,c}'."
);
Expand Down Expand Up @@ -755,6 +773,7 @@ public boolean isSuitable(String value) {

private void ensureValueSection(String fieldName) {
if (parameter) {
logger.error("Can't read field '" + fieldName + "' of non-value section '" + section + "'.");
throw new IllegalStateException(String.format(
"Can't read field '%s' of non-value section '%s'.", fieldName, section
));
Expand All @@ -763,6 +782,7 @@ private void ensureValueSection(String fieldName) {

private void ensureParameterSection(String fieldName) {
if (!parameter) {
logger.error("Can't read field '" + fieldName + "' of non-parameter section '" + section + "'.");
throw new IllegalStateException(String.format(
"Can't read field '%s' of non-parameter section '%s'.", fieldName, section
));
Expand Down Expand Up @@ -988,6 +1008,8 @@ public boolean isSuitable(String value) {
}
};
} else {
logger.error("Link section '" + section + "' contains unsupported parameter restriction '"
+ restrictionRule + "'.");
throw new ConfigurationException(String.format(
"Link section '%s' contains unsupported parameter restriction '%s'.",
section, restrictionRule
Expand All @@ -1006,13 +1028,15 @@ public static void addInterceptor(String name, Interceptor interceptor) {
ensureInterceptorName(name);

if (interceptor == null) {
throw new IllegalArgumentException("Argument \'interceptor\' is \'null\'.");
logger.error("Argument 'interceptor' is 'null'.");
throw new IllegalArgumentException("Argument 'interceptor' is 'null'.");
}

interceptorSemaphore.acquireUninterruptibly(INTERCEPTOR_MAX_PERMIT_COUNT);
try {
if (interceptorByNameMap.containsKey(name)) {
throw new IllegalStateException("Interceptor with name \'" + name + "\' already added.");
logger.error("Interceptor with name '" + name + "' already added.");
throw new IllegalStateException("Interceptor with name '" + name + "' already added.");
}
interceptorByNameMap.put(name, interceptor);
} finally {
Expand Down Expand Up @@ -1055,7 +1079,8 @@ public static boolean hasInterceptor(String name) {

private static void ensureInterceptorName(String name) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("Argument \'name\' is \'null\' or empty.");
logger.error("Argument 'name' is 'null' or empty.");
throw new IllegalArgumentException("Argument 'name' is 'null' or empty.");
}
}

Expand Down
25 changes: 25 additions & 0 deletions code/src/main/java/org/nocturne/main/ActionMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* @author Mike Mirzayanov
*/
class ActionMap {
private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(ActionMap.class);

/* Default action has empty key "". */
private final Map<String, ActionMethod> actions = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -55,6 +57,8 @@ class ActionMap {
private void processMethodAsDefault(FastClass clazz, Method method) {
if (!actions.containsKey("") && "action".equals(method.getName()) && method.getParameterTypes().length == 0) {
if (method.getReturnType() != void.class) {
logger.error("Default action method [name=" + method.getName() + ", " +
"class=" + clazz.getName() + "] should return void.");
throw new ConfigurationException("Default action method [name=" + method.getName() + ", " +
"class=" + clazz.getName() + "] should return void.");
}
Expand All @@ -63,6 +67,8 @@ private void processMethodAsDefault(FastClass clazz, Method method) {

if (!validators.containsKey("") && "validate".equals(method.getName()) && method.getParameterTypes().length == 0) {
if (method.getReturnType() != boolean.class) {
logger.error("Default validation method [name=" + method.getName() + ", " +
"class=" + clazz.getName() + "] should return boolean.");
throw new ConfigurationException("Default validation method [name=" + method.getName() + ", " +
"class=" + clazz.getName() + "] should return boolean.");
}
Expand All @@ -71,6 +77,8 @@ private void processMethodAsDefault(FastClass clazz, Method method) {

if (!invalids.containsKey("") && "invalid".equals(method.getName()) && method.getParameterTypes().length == 0) {
if (method.getReturnType() != void.class) {
logger.error("Default invalid method [name=" + method.getName() + ", " +
"class=" + clazz.getName() + "] should return void.");
throw new ConfigurationException("Default invalid method [name=" + method.getName() + ", " +
"class=" + clazz.getName() + "] should return void.");
}
Expand All @@ -80,6 +88,7 @@ private void processMethodAsDefault(FastClass clazz, Method method) {

private static void ensureProperlyAnnotatedParameters(Method method) {
if (method.getParameterTypes().length != method.getParameterAnnotations().length) {
logger.error("Expected \"method.getParameterTypes().length != method.getParameterAnnotations().length\".");
throw new NocturneException("Expected \"method.getParameterTypes().length != method.getParameterAnnotations().length\".");
}

Expand All @@ -94,10 +103,14 @@ private static void ensureProperlyAnnotatedParameters(Method method) {
}
}
if (!hasParameter) {
logger.error("Each parameter of the method " + method.getDeclaringClass().getName()
+ '#' + method.getName() + " should be annotated with @Parameter.");
throw new ConfigurationException("Each parameter of the method " + method.getDeclaringClass().getName()
+ '#' + method.getName() + " should be annotated with @Parameter.");
}
if (!hasNamedParameter) {
logger.error("Each @Parameter in the method " + method.getDeclaringClass().getName()
+ '#' + method.getName() + " should have name.");
throw new ConfigurationException("Each @Parameter in the method " + method.getDeclaringClass().getName()
+ '#' + method.getName() + " should have name.");
}
Expand All @@ -109,13 +122,17 @@ private void processMethod(FastClass clazz, Method method) {

if (action != null) {
if (actions.containsKey(action.value())) {
logger.error("There are two or more methods for " +
clazz.getName() + " marked with @Action[" + action.value() + "].");
throw new ConfigurationException("There are two or more methods for " +
clazz.getName() + " marked with @Action[" + action.value() + "].");
}

ensureProperlyAnnotatedParameters(method);

if (method.getReturnType() != void.class) {
logger.error("Method with annotation @Action [name=" + method.getName() + ", " +
"class=" + clazz.getName() + "] should return void.");
throw new ConfigurationException("Method with annotation @Action [name=" + method.getName() + ", " +
"class=" + clazz.getName() + "] should return void.");
}
Expand All @@ -127,13 +144,17 @@ private void processMethod(FastClass clazz, Method method) {

if (validate != null) {
if (validators.containsKey(validate.value())) {
logger.error("There are two or more methods for " +
clazz.getName() + " marked with @Validate[" + validate.value() + "].");
throw new ConfigurationException("There are two or more methods for " +
clazz.getName() + " marked with @Validate[" + validate.value() + "].");
}

ensureProperlyAnnotatedParameters(method);

if (method.getReturnType() != boolean.class) {
logger.error("Method with annotation @Validate [name=" + method.getName() + ", " +
"class=" + clazz.getName() + "] should return boolean.");
throw new ConfigurationException("Method with annotation @Validate [name=" + method.getName() + ", " +
"class=" + clazz.getName() + "] should return boolean.");
}
Expand All @@ -145,13 +166,17 @@ private void processMethod(FastClass clazz, Method method) {

if (invalid != null) {
if (invalids.containsKey(invalid.value())) {
logger.error("There are two or more methods for " +
clazz.getName() + " marked with @Invalid[" + invalid.value() + "].");
throw new ConfigurationException("There are two or more methods for " +
clazz.getName() + " marked with @Invalid[" + invalid.value() + "].");
}

ensureProperlyAnnotatedParameters(method);

if (method.getReturnType() != void.class) {
logger.error("Method with annotation @Invalid [name=" + method.getName() + ", " +
"class=" + clazz.getName() + "] should return void.");
throw new ConfigurationException("Method with annotation @Invalid [name=" + method.getName() + ", " +
"class=" + clazz.getName() + "] should return void.");
}
Expand Down
Loading