001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.resource.loader; 018 019import java.lang.ref.WeakReference; 020import java.util.Locale; 021 022import org.apache.wicket.Component; 023import org.apache.wicket.util.lang.Args; 024 025/** 026 * This string resource loader attempts to find a single resource bundle that has the same name and 027 * location as the clazz provided in the constructor. If the bundle is found than strings are 028 * obtained from here. This implementation is fully aware of both locale and style values when 029 * trying to obtain the appropriate bundle. 030 * <p> 031 * An instance of this loader is registered with the Application by default. 032 * 033 * @author Chris Turner 034 * @author Juergen Donnerstag 035 */ 036public class ClassStringResourceLoader extends ComponentStringResourceLoader 037{ 038 /** The application we are loading for. */ 039 private final WeakReference<Class<?>> clazzRef; 040 041 /** 042 * Create and initialize the resource loader. 043 * 044 * @param clazz 045 * The class that this resource loader is associated with 046 */ 047 public ClassStringResourceLoader(final Class<?> clazz) 048 { 049 Args.notNull(clazz, "clazz"); 050 clazzRef = new WeakReference<>(clazz); 051 } 052 053 @Override 054 public String loadStringResource(final Class<?> clazz, final String key, final Locale locale, 055 final String style, final String variation) 056 { 057 return super.loadStringResource(clazzRef.get(), key, locale, style, variation); 058 } 059 060 @Override 061 public String loadStringResource(final Component component, final String key, 062 final Locale locale, final String style, final String variation) 063 { 064 if (component == null) 065 { 066 return super.loadStringResource(clazzRef.get(), key, locale, style, variation); 067 } 068 return super.loadStringResource(component, key, locale, style, variation); 069 } 070}