String identifier = XPathHelper.valueOf(config, "@id", xpathProcessor);
if (identifier == null)
throw new IllegalStateException("Unable to create sites without identifier");
// class
Site site = null;
String className = XPathHelper.valueOf(config, "ns:class", xpathProcessor);
if (className != null) {
try {
Class<? extends Site> c = (Class<? extends Site>) classLoader.loadClass(className);
site = c.newInstance();
site.setIdentifier(identifier);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Implementation " + className + " for site '" + identifier + "' not found", e);
} catch (InstantiationException e) {
throw new IllegalStateException("Error instantiating impelementation " + className + " for site '" + identifier + "'", e);
} catch (IllegalAccessException e) {
throw new IllegalStateException("Access violation instantiating implementation " + className + " for site '" + identifier + "'", e);
} catch (Throwable t) {
throw new IllegalStateException("Error loading implementation " + className + " for site '" + identifier + "'", t);
}
} else {
site = new SiteImpl();
site.setIdentifier(identifier);
}
// name
String name = XPathHelper.valueOf(config, "ns:name", xpathProcessor);
if (name == null)
throw new IllegalStateException("Site '" + identifier + " has no name");
site.setName(name);
// domains
NodeList urlNodes = XPathHelper.selectList(config, "ns:domains/ns:url", xpathProcessor);
if (urlNodes.getLength() == 0)
throw new IllegalStateException("Site '" + identifier + " has no hostname");
String url = null;
try {
for (int i = 0; i < urlNodes.getLength(); i++) {
Node urlNode = urlNodes.item(i);
url = urlNode.getFirstChild().getNodeValue();
boolean defaultUrl = ConfigurationUtils.isDefault(urlNode);
Environment environment = Environment.Production;
Node environmentAttribute = urlNode.getAttributes().getNamedItem("environment");
if (environmentAttribute != null && environmentAttribute.getNodeValue() != null)
environment = Environment.valueOf(StringUtils.capitalize(environmentAttribute.getNodeValue()));
SiteURLImpl siteURL = new SiteURLImpl(new URL(url));
siteURL.setDefault(defaultUrl);
siteURL.setEnvironment(environment);
site.addHostname(siteURL);
}
} catch (MalformedURLException e) {
throw new IllegalStateException("Site '" + identifier + "' defines malformed url: " + url);
}
// languages
NodeList languageNodes = XPathHelper.selectList(config, "ns:languages/ns:language", xpathProcessor);
if (languageNodes.getLength() == 0)
throw new IllegalStateException("Site '" + identifier + " has no languages");
for (int i = 0; i < languageNodes.getLength(); i++) {
Node languageNode = languageNodes.item(i);
Node defaultAttribute = languageNode.getAttributes().getNamedItem("default");
String languageId = languageNode.getFirstChild().getNodeValue();
try {
Language language = LanguageUtils.getLanguage(languageId);
if (ConfigurationUtils.isTrue(defaultAttribute))
site.setDefaultLanguage(language);
else
site.addLanguage(language);
} catch (UnknownLanguageException e) {
throw new IllegalStateException("Site '" + identifier + "' defines unknown language: " + languageId);
}
}
// templates
NodeList templateNodes = XPathHelper.selectList(config, "ns:templates/ns:template", xpathProcessor);
PageTemplate firstTemplate = null;
for (int i = 0; i < templateNodes.getLength(); i++) {
PageTemplate template = PageTemplateImpl.fromXml(templateNodes.item(i), xpathProcessor);
boolean isDefault = ConfigurationUtils.isDefault(templateNodes.item(i));
if (isDefault && site.getDefaultTemplate() != null) {
logger.warn("Site '{}' defines more than one default templates", site.getIdentifier());
} else if (isDefault) {
site.setDefaultTemplate(template);
logger.debug("Site '{}' uses default template '{}'", site.getIdentifier(), template.getIdentifier());
} else {
site.addTemplate(template);
logger.debug("Added template '{}' to site '{}'", template.getIdentifier(), site.getIdentifier());
}
if (firstTemplate == null)
firstTemplate = template;
}
// Make sure we have a default template
if (site.getDefaultTemplate() == null) {
if (firstTemplate == null)
throw new IllegalStateException("Site '" + site.getIdentifier() + "' does not specify any page templates");
logger.warn("Site '{}' does not specify a default template. Using '{}'", site.getIdentifier(), firstTemplate.getIdentifier());
site.setDefaultTemplate(firstTemplate);
}
// security
String securityConfiguration = XPathHelper.valueOf(config, "ns:security/ns:configuration", xpathProcessor);
if (securityConfiguration != null) {
URL securityConfig = null;
// If converting the path into a URL fails, we are assuming that the
// configuration is part of the bundle
try {
securityConfig = new URL(securityConfiguration);
} catch (MalformedURLException e) {
logger.debug("Security configuration {} is pointing to the bundle", securityConfiguration);
securityConfig = SiteImpl.class.getResource(securityConfiguration);
if (securityConfig == null) {
throw new IllegalStateException("Security configuration " + securityConfig + " of site '" + site.getIdentifier() + "' cannot be located inside of bundle", e);
}
}
site.setSecurity(securityConfig);
}
// administrator
Node adminNode = XPathHelper.select(config, "ns:security/ns:administrator", xpathProcessor);
if (adminNode != null) {
site.setAdministrator(SiteAdminImpl.fromXml(adminNode, site, xpathProcessor));
}
// digest policy
Node digestNode = XPathHelper.select(config, "ns:security/ns:digest", xpathProcessor);
if (digestNode != null) {
site.setDigestType(DigestType.valueOf(digestNode.getFirstChild().getNodeValue()));
}
// role definitions
NodeList roleNodes = XPathHelper.selectList(config, "ns:security/ns:roles/ns:*", xpathProcessor);
for (int i = 0; i < roleNodes.getLength(); i++) {
Node roleNode = roleNodes.item(i);
String roleName = roleNode.getLocalName();
String localRoleName = roleNode.getFirstChild().getNodeValue();
if (Security.SITE_ADMIN_ROLE.equals(roleName))
site.addLocalRole(Security.SITE_ADMIN_ROLE, localRoleName);
if (Security.PUBLISHER_ROLE.equals(roleName))
site.addLocalRole(Security.PUBLISHER_ROLE, localRoleName);
if (Security.EDITOR_ROLE.equals(roleName))
site.addLocalRole(Security.EDITOR_ROLE, localRoleName);
if (Security.GUEST_ROLE.equals(roleName))
site.addLocalRole(Security.GUEST_ROLE, localRoleName);
}
// options
Node optionsNode = XPathHelper.select(config, "ns:options", xpathProcessor);
OptionsHelper.fromXml(optionsNode, site, xpathProcessor);