public static void initVelocity() throws InitializationFailedException { logger.debug("-------------------------------------------------------"); logger.debug("Initializing Velocity"); // Creating velocity engine velocityEngine = new VelocityEngine(); Properties bProperties = Property.getProps("velocity.properties"); bProperties.put( Velocity.FILE_RESOURCE_LOADER_PATH, Property.get("PATH_TO_TEMPLATES") + "," + Property.get("PATH_TO_TEMPLATES") + "includes/"); logger.debug( "> Initializing velocity engine with FILE_RESOURCE_LOADER_PATH: " + Property.get("PATH_TO_TEMPLATES")); try { velocityEngine.init(bProperties); logger.debug("> Velocity engine successfully initialized"); } catch (Exception e) { throw new InitializationFailedException( "Error instanciating velocity engine: " + e.getMessage()); } // Creating velocity contexts for (ULocale locale : languageProperties.keySet()) { VelocityContext context = new VelocityContext(); Properties langProps = languageProperties.get(locale); for (Object key : langProps.keySet()) { context.put((String) key, langProps.get(key)); } context.put("esc", new EscapeTool()); context.put("math", new MathTool()); context.put("unicornTool", new UnicornVelocityTool()); context.put("ucn", new Language()); context.put("strUtils", new StringUtils()); context.put("tasklist", mapOfTask); context.put("param_prefix", Property.get("UNICORN_PARAMETER_PREFIX")); context.put("languages", Language.getUiLocales()); context.put("lang", locale.getName()); context.put("currentLocale", locale); context.put("direction", Language.getLocaleDirection(locale)); context.put("defaultLocale", Language.getDefaultLocale()); context.put("year", (new SimpleDateFormat("yyyy")).format(new Date())); languageContexts.put(locale, context); } logger.debug("> " + languageContexts.size() + " velocity context(s) created"); logger.info("OK - Velocity successfully initialized"); }
public static void reset() { unicornPropertiesFiles = new Hashtable<String, UCNProperties>(); languageContexts = new Hashtable<ULocale, VelocityContext>(); languageProperties = new Hashtable<ULocale, UCNProperties>(); metadataProperties = new Hashtable<ULocale, UCNProperties>(); mapOfObserver = new LinkedHashMap<String, Observer>(); responseImpl = new LinkedHashMap<String, Class<Response>>(); Language.reset(); }
public static void initTasklists() throws InitializationFailedException { logger.debug("-------------------------------------------------------"); logger.debug( "Loading xml task files from tasklist directory: " + Property.get("PATH_TO_TASKLIST")); TasksListUnmarshaller aTaskListUnmarshaller = new TaskListUnmarshallerBeans(mapOfObserver); File[] tFileXML = ListFiles.listFiles(Property.get("PATH_TO_TASKLIST"), "\\.xml$"); for (File aFile : tFileXML) { try { logger.debug("- Loading xml file: " + aFile.getName()); aTaskListUnmarshaller.addURL(aFile.toURI().toURL()); aTaskListUnmarshaller.unmarshal(); } catch (MalformedURLException e) { logger.error(e.getMessage(), e); } catch (IOException e) { logger.error("Error reading file: " + aFile.getName(), e); logger.warn("> This task file will be skiped"); } catch (Exception e) { logger.error("Error unmarshalling file: " + aFile.getName(), e); logger.warn("> This task file will be skiped"); } } mapOfTask = aTaskListUnmarshaller.getMapOfTask(); if (mapOfTask.size() == 0) { throw new InitializationFailedException( "No task have been loaded. Check task files in: " + Property.get("PATH_TO_TASKLIST")); } else { String s = "Map of tasks:"; logger.debug(s + mapOfTask); logger.info("OK - " + mapOfTask.size() + " task(s) successfully loaded."); } logger.debug("-------------------------------------------------------"); logger.debug( "Loading tasks metadata files from tasks language directory: " + Property.get("PATH_TO_TASK_LANGUAGE_FILES")); File defaultTaskFile = new File( Property.get("PATH_TO_TASK_LANGUAGE_FILES", "DEFAULT_LANGUAGE") + ".tasklist.properties"); String defaultLang = Language.getDefaultLocale().getName(); UCNProperties defaultProps = null; try { defaultProps = Language.load(defaultTaskFile); defaultProps.parse(); logger.debug("> Found default tasks metadata file: " + defaultTaskFile.getPath()); LanguageAction.addMetadatasProperties(Language.getDefaultLocale(), defaultProps); metadataProperties.put(Language.getDefaultLocale(), defaultProps); for (String taskKey : mapOfTask.keySet()) { Task task = mapOfTask.get(taskKey); if (defaultProps.containsKey(taskKey)) task.addLongName(defaultLang, defaultProps.getProperty(taskKey)); else { task.addLongName(defaultLang, taskKey); logger.warn( ">> No default longname found for the task: " + taskKey + ". This task longname will be its key and it won't appear in the translation form."); } if (defaultProps.containsKey(taskKey + ".description")) task.addDescription(defaultLang, defaultProps.getProperty(taskKey + ".description")); else logger.warn( ">> No default description found for the task: " + taskKey + ". This task description will be empty and it won't appear in the translation form."); Map<String, Parameter> params = task.getMapOfParameter(); for (Object paramKey : params.keySet()) { Parameter param = (Parameter) params.get(paramKey); if (defaultProps.containsKey(taskKey + ".param." + paramKey)) param.addLongName( defaultLang, defaultProps.getProperty(taskKey + ".param." + paramKey)); else if (defaultProps.containsKey("param." + paramKey)) param.addLongName(defaultLang, defaultProps.getProperty("param." + paramKey)); else logger.warn( ">> No default parameter longname found for the parameter: " + paramKey + ". This parameter longname will be its key and it won't appear in the translation form."); Map<String, Value> values = param.getMapOfValue(); for (Object valueKey : values.keySet()) { Value value = (Value) values.get(valueKey); if (defaultProps.containsKey(taskKey + ".param." + paramKey + "." + valueKey)) value.addLongName( defaultLang, defaultProps.getProperty(taskKey + ".param." + paramKey + "." + valueKey)); else if (defaultProps.containsKey("param." + paramKey + "." + valueKey)) value.addLongName( defaultLang, defaultProps.getProperty("param." + paramKey + "." + valueKey)); else if (param.getUiLevel() != TUi.NONE && !value.hasLongName( defaultLang)) { // Warn only if the value is displayed and option name is not // defined directly in contract value.addLongName(defaultLang, value.getName()); logger.warn( ">> No default value longname found for the value \"" + valueKey + "\" for the parameter \"" + paramKey + "\" from the task " + taskKey + ". This value longname will be its key and it won't appear in the translation form."); } } } } } catch (IllegalArgumentException e) { logger.warn(e.getMessage()); } catch (FileNotFoundException e) { throw new InitializationFailedException( "Default tasks metadata file does not exist: " + defaultTaskFile.getPath()); } catch (IOException e) { throw new InitializationFailedException( "Unable to read default tasks metadata file " + defaultTaskFile); } File[] taskFiles = ListFiles.listFiles(Property.get("PATH_TO_TASK_LANGUAGE_FILES"), "\\.tasklist.properties$"); for (File taskFile : taskFiles) { if (taskFile.equals(defaultTaskFile)) continue; try { ULocale fileLocale = Language.getLocaleFromFileName(taskFile.getName()); String lang = fileLocale.getName(); UCNProperties props = Language.load(taskFile); props.parse(); logger.debug("> Found tasks metadata file: " + taskFile.getPath()); ArrayList<Object> keys = new ArrayList<Object>(); for (Object key : props.keySet()) { if (defaultProps != null && !defaultProps.containsKey(key)) { keys.add(key); logger.warn( ">> Unknown key \"" + key + "\" found in " + taskFile.getPath() + ". Please remove this key manually from the tasklist properties file."); } } for (Object key : keys) props.remove(key); LanguageAction.addMetadatasProperties(fileLocale, props); metadataProperties.put(fileLocale, props); for (String taskKey : mapOfTask.keySet()) { Task task = mapOfTask.get(taskKey); if (props.containsKey(taskKey)) task.addLongName(lang, props.getProperty(taskKey)); if (props.containsKey(taskKey + ".description")) task.addDescription(lang, props.getProperty(taskKey + ".description")); Map<String, Parameter> params = task.getMapOfParameter(); for (Object paramKey : params.keySet()) { Parameter param = (Parameter) params.get(paramKey); if (props.containsKey(taskKey + ".param." + paramKey)) param.addLongName(lang, props.getProperty(taskKey + ".param." + paramKey)); else if (props.containsKey("param." + paramKey)) param.addLongName(lang, props.getProperty("param." + paramKey)); Map<String, Value> values = param.getMapOfValue(); for (Object valueKey : values.keySet()) { Value value = (Value) values.get(valueKey); if (props.containsKey(taskKey + ".param." + paramKey + "." + valueKey)) value.addLongName( lang, props.getProperty(taskKey + ".param." + paramKey + "." + valueKey)); else if (props.containsKey("param." + paramKey + "." + valueKey)) value.addLongName(lang, props.getProperty("param." + paramKey + "." + valueKey)); } } } } catch (IllegalArgumentException e) { logger.warn(e.getMessage()); } catch (FileNotFoundException e) { // Should not happen logger.error(e.getMessage(), e); } catch (IOException e) { logger.error("Unable to read task file " + taskFile + ". This file will be skiped."); } } }
public static void initLanguages() throws InitializationFailedException { // Loading language files logger.debug("-------------------------------------------------------"); logger.debug( "Loading language files from language directory: " + Property.get("PATH_TO_LANGUAGE_FILES")); ULocale defaultLocale = null; for (ULocale locale : ULocale.getAvailableLocales()) { if (locale.getName().equals(Property.get("DEFAULT_LANGUAGE"))) { defaultLocale = locale; break; } } if (defaultLocale == null) throw new InitializationFailedException( "Locale not found for default language in unicorn.properties: " + Property.get("DEFAULT_LANGUAGE")); Language.reset(); Language.initLocaleMatcher(defaultLocale); File defaultLanguageFile = new File(Property.get("PATH_TO_LANGUAGE_FILES", "DEFAULT_LANGUAGE") + ".properties"); UCNProperties defaultProps = new UCNProperties(); try { defaultProps = Language.load(defaultLanguageFile); logger.debug( "> Found language (default): " + defaultLocale.getName() + " - " + defaultLocale.getDisplayName(defaultLocale)); Language.addUiLocale(defaultLocale); LanguageAction.addLanguageProperties(defaultLocale, defaultProps); defaultProps.parse(); defaultProps.put("complete", "true"); languageProperties.put(defaultLocale, defaultProps); UCNProperties metaProps = new UCNProperties(); LanguageAction.addMetadatasProperties(defaultLocale, metaProps); metadataProperties.put(defaultLocale, metaProps); } catch (IllegalArgumentException e) { logger.warn(e.getMessage()); } catch (FileNotFoundException e) { throw new InitializationFailedException( "Default language file does not exist: " + defaultLanguageFile.getPath()); } catch (IOException e) { throw new InitializationFailedException( "Unable to read default language file " + defaultLanguageFile.getPath()); } File[] languageFiles = ListFiles.listFiles(Property.get("PATH_TO_LANGUAGE_FILES"), "\\.properties$"); for (File langFile : languageFiles) { if (langFile.equals(defaultLanguageFile)) continue; try { ULocale fileLocale = Language.getLocaleFromFileName(langFile.getName()); if (fileLocale == Language.getDefaultLocale()) continue; UCNProperties props = Language.load(langFile); logger.debug( "> Found language: " + fileLocale.getName() + " - " + fileLocale.getDisplayName(Language.getDefaultLocale())); Language.addUiLocale(fileLocale); Language.clean(props, defaultProps, langFile.getName()); LanguageAction.addLanguageProperties(fileLocale, props); Language.complete(props, defaultProps, langFile.getName()); props.parse(); languageProperties.put(fileLocale, props); UCNProperties metaProps = new UCNProperties(); LanguageAction.addMetadatasProperties(fileLocale, metaProps); metadataProperties.put(fileLocale, metaProps); } catch (IllegalArgumentException e) { logger.warn(e.getMessage()); } catch (FileNotFoundException e) { // Should not happen logger.error(e.getMessage(), e); } catch (IOException e) { logger.error("Unable to read language file " + langFile + ". This file will be skiped."); } } Language.initUILocaleMatcher(); if (languageProperties.size() == 0) { throw new InitializationFailedException( "No language have been loaded. Check language files in: " + Property.get("PATH_TO_LANGUAGE_FILES")); } else { String s = "Language properties:"; for (ULocale localeKey : languageProperties.keySet()) { Properties props = languageProperties.get(localeKey); s += "\n\n\t" + localeKey.getDisplayName(Language.getDefaultLocale()) + ":"; for (Object langKey : props.keySet()) { s += "\n\t\t" + langKey + " => " + props.getProperty((String) langKey); } } logger.debug(s); logger.info("OK - " + languageProperties.size() + " language(s) successfully loaded."); } }