diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/RegistryBasedResourceLoader.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/RegistryBasedResourceLoader.java index 7ba350e07c9..8b12607fc61 100644 --- a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/RegistryBasedResourceLoader.java +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/RegistryBasedResourceLoader.java @@ -27,7 +27,7 @@ import org.wso2.carbon.context.RegistryType; import org.wso2.carbon.registry.api.Registry; import org.wso2.carbon.registry.api.RegistryException; -import java.io.*; +import java.io.InputStream; public class RegistryBasedResourceLoader extends ResourceLoader { @@ -46,12 +46,12 @@ public class RegistryBasedResourceLoader extends ResourceLoader { if (registry == null) { throw new IllegalStateException("No valid registry instance is attached to the current carbon context"); } - if (!registry.resourceExists(EMAIL_CONFIG_BASE_LOCATION + "/" + name + ".vm")) { + if (!registry.resourceExists(EMAIL_CONFIG_BASE_LOCATION + "/" + name)) { throw new ResourceNotFoundException("Resource '" + name + "' does not exist"); } org.wso2.carbon.registry.api.Resource resource = - registry.get(EMAIL_CONFIG_BASE_LOCATION + "/" + name + ".vm"); - + registry.get(EMAIL_CONFIG_BASE_LOCATION + "/" + name); + resource.setMediaType("text/plain"); return resource.getContentStream(); } catch (RegistryException e) { throw new ResourceNotFoundException("Error occurred while retrieving resource", e); diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderAxis2ConfigContextObserver.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderAxis2ConfigContextObserver.java index 4bd0cc5ccac..0a4cea270ca 100644 --- a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderAxis2ConfigContextObserver.java +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderAxis2ConfigContextObserver.java @@ -21,22 +21,11 @@ package org.wso2.carbon.email.sender.core.internal; import org.apache.axis2.context.ConfigurationContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.context.CarbonContext; -import org.wso2.carbon.context.RegistryType; import org.wso2.carbon.email.sender.core.EmailSenderConfigurationFailedException; -import org.wso2.carbon.registry.api.Collection; -import org.wso2.carbon.registry.api.Registry; -import org.wso2.carbon.registry.api.RegistryException; -import org.wso2.carbon.registry.api.Resource; import org.wso2.carbon.utils.Axis2ConfigurationContextObserver; -import org.wso2.carbon.utils.CarbonUtils; -import java.io.File; -import java.io.FilenameFilter; +class EmailSenderAxis2ConfigContextObserver implements Axis2ConfigurationContextObserver { -public class EmailSenderAxis2ConfigContextObserver implements Axis2ConfigurationContextObserver { - - private static final String EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH = "email-templates"; private static final Log log = LogFactory.getLog(EmailSenderAxis2ConfigContextObserver.class); @Override @@ -47,7 +36,7 @@ public class EmailSenderAxis2ConfigContextObserver implements Axis2Configuration @Override public void createdConfigurationContext(ConfigurationContext configurationContext) { try { - this.setupEmailTemplates(); + EmailUtils.setupEmailTemplates(); } catch (EmailSenderConfigurationFailedException e) { log.error("Error occurred while setting up email templates", e); } @@ -63,50 +52,4 @@ public class EmailSenderAxis2ConfigContextObserver implements Axis2Configuration } - private void setupEmailTemplates() throws EmailSenderConfigurationFailedException { - File templateDir = - new File(CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "resources" - + File.separator + "email-templates"); - if (!templateDir.exists()) { - if (log.isDebugEnabled()) { - log.debug("The directory that is expected to use as the container for all email templates is not " + - "available. Therefore, no template is uploaded to the registry"); - } - } - if (templateDir.canRead()) { - File[] templates = templateDir.listFiles(new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - name = name.toLowerCase(); - return name.endsWith(".vm"); - } - }); - try { - Registry registry = - CarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.SYSTEM_CONFIGURATION); - if (!registry.resourceExists(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH)) { - Collection collection = registry.newCollection(); - registry.put(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH, collection); - for (File template : templates) { - Resource resource = registry.newResource(); - resource.setContent(template); - registry.put(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/" + template.getName(), resource); - } - } else { - for (File template : templates) { - if (!registry.resourceExists( - EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/" + template.getName())) { - Resource resource = registry.newResource(); - resource.setContent(template); - registry.put( - EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/" + template.getName(), resource); - } - } - } - } catch (RegistryException e) { - throw new EmailSenderConfigurationFailedException("Error occurred while setting up email templates", e); - } - } - } - } diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderServiceComponent.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderServiceComponent.java index 38b8e451dd1..3d4e3933ed7 100644 --- a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderServiceComponent.java +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailSenderServiceComponent.java @@ -17,28 +17,16 @@ */ package org.wso2.carbon.email.sender.core.internal; -import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.service.component.ComponentContext; import org.wso2.carbon.email.sender.core.EmailSenderConfig; -import org.wso2.carbon.email.sender.core.EmailSenderConfigurationFailedException; import org.wso2.carbon.email.sender.core.service.EmailSenderService; import org.wso2.carbon.email.sender.core.service.EmailSenderServiceImpl; -import org.wso2.carbon.registry.api.Collection; -import org.wso2.carbon.registry.api.Registry; -import org.wso2.carbon.registry.api.RegistryException; -import org.wso2.carbon.registry.api.Resource; import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.utils.Axis2ConfigurationContextObserver; -import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.ConfigurationContextService; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FilenameFilter; -import java.io.IOException; - /** * @scr.component name="org.wso2.carbon.email.sender.EmailSenderServiceComponent" immediate="true" * @scr.reference name="registry.service" @@ -56,7 +44,6 @@ import java.io.IOException; */ public class EmailSenderServiceComponent { - private static final String EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH = "/email-templates"; private static Log log = LogFactory.getLog(EmailSenderServiceComponent.class); @SuppressWarnings("unused") @@ -69,7 +56,7 @@ public class EmailSenderServiceComponent { EmailSenderConfig.init(); /* Setting up default email templates */ - this.setupEmailTemplates(); + EmailUtils.setupEmailTemplates(); /* Registering declarative service instances exposed by EmailSenderServiceComponent */ this.registerServices(componentContext); @@ -98,64 +85,6 @@ public class EmailSenderServiceComponent { componentContext.getBundleContext().registerService(EmailSenderService.class, emailServiceProvider, null); } - private void setupEmailTemplates() throws EmailSenderConfigurationFailedException { - File templateDir = - new File(CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + - "resources" + File.separator + "email-templates"); - if (!templateDir.exists()) { - if (log.isDebugEnabled()) { - log.debug("The directory that is expected to use as the container for all email templates is not " + - "available. Therefore, no template is uploaded to the registry"); - } - } - if (templateDir.canRead()) { - File[] templates = templateDir.listFiles(new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - name = name.toLowerCase(); - return name.endsWith(".vm"); - } - }); - try { - Registry registry = - EmailSenderDataHolder.getInstance().getRegistryService().getConfigSystemRegistry(); - if (!registry.resourceExists(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH)) { - Collection collection = registry.newCollection(); - registry.put(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH, collection); - for (File template : templates) { - Resource resource = registry.newResource(); - String contents = FileUtils.readFileToString(template); - resource.setContent(contents.getBytes()); - registry.put(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/" + template.getName(), resource); - } - } else { - /* Existence of a given resource is not checked consciously, before performing registry.put() below. - * The rationale is that, the only less expensive way that one can check if a resource exists is - * that through registry.resourceExists(), which only checks if 'some' resource exists at the given - * registry path. However, this does not capture scenarios where there can be updated contents to - * the same resource of which the path hasn't changed after it has been initialized for the first - * time. Therefore, whenever the server starts-up, all email templates are updated just to avoid - * the aforementioned problem */ - for (File template : templates) { - Resource resource = registry.newResource(); - String contents = FileUtils.readFileToString(template); - resource.setContent(contents.getBytes()); - registry.put( - EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/" + template.getName(), resource); - } - } - } catch (RegistryException e) { - throw new EmailSenderConfigurationFailedException("Error occurred while setting up email templates", e); - } catch (FileNotFoundException e) { - throw new EmailSenderConfigurationFailedException("Error occurred while writing template file " + - "contents as an input stream of a resource", e); - } catch (IOException e) { - throw new EmailSenderConfigurationFailedException("Error occurred while serializing file " + - "contents to a string", e); - } - } - } - /** * Sets Registry Service. * diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailUtils.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailUtils.java new file mode 100644 index 00000000000..8088d95503f --- /dev/null +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/internal/EmailUtils.java @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.email.sender.core.internal; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.email.sender.core.EmailSenderConfigurationFailedException; +import org.wso2.carbon.registry.api.Collection; +import org.wso2.carbon.registry.api.Registry; +import org.wso2.carbon.registry.api.RegistryException; +import org.wso2.carbon.registry.api.Resource; +import org.wso2.carbon.utils.CarbonUtils; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FilenameFilter; +import java.io.IOException; + +class EmailUtils { + + private static final String EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH = "/email-templates"; + private static Log log = LogFactory.getLog(EmailSenderServiceComponent.class); + + static void setupEmailTemplates() throws EmailSenderConfigurationFailedException { + File templateDir = + new File(CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + + "resources" + File.separator + "email-templates"); + if (!templateDir.exists()) { + if (log.isDebugEnabled()) { + log.debug("The directory that is expected to use as the container for all email templates is not " + + "available. Therefore, no template is uploaded to the registry"); + } + } + if (templateDir.canRead()) { + File[] templates = templateDir.listFiles(new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + name = name.toLowerCase(); + return name.endsWith(".vm"); + } + }); + try { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + Registry registry = + EmailSenderDataHolder.getInstance().getRegistryService().getConfigSystemRegistry(tenantId); + if (!registry.resourceExists(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH)) { + Collection collection = registry.newCollection(); + registry.put(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH, collection); + for (File template : templates) { + Resource resource = registry.newResource(); + resource.setMediaType("text/plain"); + String contents = FileUtils.readFileToString(template); + resource.setContent(contents); + registry.put(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/" + + template.getName().replace(".vm", ""), resource); + } + } + } catch (RegistryException e) { + throw new EmailSenderConfigurationFailedException("Error occurred while setting up email templates", e); + } catch (FileNotFoundException e) { + throw new EmailSenderConfigurationFailedException("Error occurred while writing template file " + + "contents as an input stream of a resource", e); + } catch (IOException e) { + throw new EmailSenderConfigurationFailedException("Error occurred while serializing file " + + "contents to a string", e); + } + } + } + +}