| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF 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.apache.deltaspike.core.impl.message; | ||
|
|
||
| import org.apache.deltaspike.core.api.message.MessageResolver; | ||
| import org.apache.deltaspike.core.util.PropertyFileUtils; | ||
|
|
||
| import javax.enterprise.inject.Typed; | ||
| import java.util.Locale; | ||
| import java.util.MissingResourceException; | ||
| import java.util.ResourceBundle; | ||
|
|
||
| @Typed() | ||
| class DefaultMessageResolver implements MessageResolver | ||
| { | ||
| private static final long serialVersionUID = 5834411208472341006L; | ||
|
|
||
| private final ResourceBundle messageBundle; | ||
|
|
||
| DefaultMessageResolver(String messageBundleName, Locale locale) | ||
| { | ||
| ResourceBundle resolvedBundle; | ||
| try | ||
| { | ||
| resolvedBundle = PropertyFileUtils.getResourceBundle(messageBundleName, locale); | ||
| } | ||
| catch (MissingResourceException e) | ||
| { | ||
| //X TODO log it | ||
| resolvedBundle = null; | ||
| } | ||
|
|
||
| this.messageBundle = resolvedBundle; | ||
| } | ||
|
|
||
| @Override | ||
| public String getMessage(String messageDescriptor) | ||
| { | ||
| if (this.messageBundle != null && messageDescriptor != null && | ||
| messageDescriptor.startsWith("{") && messageDescriptor.endsWith("}")) | ||
| { | ||
| try | ||
| { | ||
| return this.messageBundle.getString(messageDescriptor.substring(1, messageDescriptor.length() - 1)); | ||
| } | ||
| catch (MissingResourceException e) | ||
| { | ||
| return MISSING_RESOURCE_MARKER + messageDescriptor + MISSING_RESOURCE_MARKER; | ||
| } | ||
| } | ||
|
|
||
| return messageDescriptor; | ||
| } | ||
| } |
| @@ -20,13 +20,10 @@ | ||
|
|
||
| import javax.inject.Inject; | ||
|
|
||
| public class Jay | ||
| { | ||
| @Inject | ||
| private TestMessages messages; | ||
|
|
||
| String getMessage() | ||
| { | ||
| @@ -22,8 +22,8 @@ | ||
| import org.apache.deltaspike.core.api.message.MessageBundle; | ||
|
|
||
| @MessageBundle | ||
| public interface SimpleMessage | ||
| { | ||
| @Message("Welcome to DeltaSpike") | ||
| String welcomeToDeltaSpike(); | ||
| } | ||
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF 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.apache.deltaspike.test.core.api.message; | ||
|
|
||
| import org.apache.deltaspike.core.api.provider.BeanManagerProvider; | ||
| import org.apache.deltaspike.core.impl.message.MessageBundleExtension; | ||
| import org.apache.deltaspike.test.util.ArchiveUtils; | ||
| import org.jboss.arquillian.container.test.api.Deployment; | ||
| import org.jboss.arquillian.junit.Arquillian; | ||
| import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
| import org.jboss.shrinkwrap.api.asset.EmptyAsset; | ||
| import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
| import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
|
|
||
| import javax.enterprise.inject.spi.Extension; | ||
| import javax.inject.Inject; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| /** | ||
| * Tests for {@link org.apache.deltaspike.core.api.message.Message} | ||
| */ | ||
| @RunWith(Arquillian.class) | ||
| public class SimpleMessageTest | ||
| { | ||
| @Inject | ||
| private SimpleMessage simpleMessage; | ||
|
|
||
| /** | ||
| * X TODO creating a WebArchive is only a workaround because JavaArchive | ||
| * cannot contain other archives. | ||
| */ | ||
| @Deployment | ||
| public static WebArchive deploy() | ||
| { | ||
| new BeanManagerProvider() | ||
| { | ||
| @Override | ||
| public void setTestMode() | ||
| { | ||
| super.setTestMode(); | ||
| } | ||
| }.setTestMode(); | ||
|
|
||
| JavaArchive testJar = ShrinkWrap | ||
| .create(JavaArchive.class, "messageTest.jar") | ||
| .addPackage("org.apache.deltaspike.test.core.api.message") | ||
| .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); | ||
|
|
||
| return ShrinkWrap | ||
| .create(WebArchive.class, "messageTest.war") | ||
| .addAsLibraries(ArchiveUtils.getDeltaSpikeCoreArchive()) | ||
| .addAsLibraries(testJar) | ||
| .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") | ||
| .addAsServiceProvider(Extension.class, | ||
| MessageBundleExtension.class); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSimpleMessage() | ||
| { | ||
| assertEquals("Welcome to DeltaSpike", this.simpleMessage.welcomeToDeltaSpike()); | ||
| } | ||
| } |
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF 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.apache.deltaspike.test.core.api.message; | ||
|
|
||
| import org.apache.deltaspike.core.api.message.MessageInterpolator; | ||
|
|
||
| import javax.enterprise.context.ApplicationScoped; | ||
|
|
||
| @ApplicationScoped | ||
| public class TestMessageInterpolator implements MessageInterpolator | ||
| { | ||
| @Override | ||
| public String interpolate(String messageText, Object[] arguments) | ||
| { | ||
| return String.format(messageText, arguments); | ||
| } | ||
| } |
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF 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.apache.deltaspike.test.core.api.message; | ||
|
|
||
| import org.apache.deltaspike.core.api.message.Message; | ||
| import org.apache.deltaspike.core.api.message.MessageBundle; | ||
| import org.apache.deltaspike.core.api.message.MessageContextConfig; | ||
|
|
||
| @MessageBundle | ||
| @MessageContextConfig(messageInterpolator = TestMessageInterpolator.class) | ||
| public interface TestMessages | ||
| { | ||
| @Message("Spotted %s jays") | ||
| String numberOfJaysSpotted(int number); | ||
|
|
||
| @Message("{welcome_to_deltaspike}") | ||
| String welcomeToDeltaSpike(); | ||
|
|
||
| @Message("{welcome_to}") | ||
| String welcomeTo(String name); | ||
|
|
||
| //X TODO | ||
| //@Message("{welcome_to}") | ||
| //String welcomeTo(MessageContext messageContext, String name); | ||
| } |
| @@ -0,0 +1,19 @@ | ||
| #Licensed to the Apache Software Foundation (ASF) under one | ||
| #or more contributor license agreements. See the NOTICE file | ||
| #distributed with this work for additional information | ||
| #regarding copyright ownership. The ASF 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. | ||
|
|
||
| welcome_to=Welcome to %s | ||
| welcome_to_deltaspike=Welcome to DeltaSpike |