Skip to content

Commit

Permalink
SAK-43462 Kernel Resource should be less verbose (#8065)
Browse files Browse the repository at this point in the history
In this change Resource class was instatiated everywhere with no
real benefit. This change moves to a static implementation.
  • Loading branch information
ern committed Mar 24, 2020
1 parent e984a38 commit 0531dca
Show file tree
Hide file tree
Showing 26 changed files with 49 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -633,8 +633,8 @@ public void init() throws ServletException {

String resourceClass = scs.getString(RESOURCECLASS, DEFAULT_RESOURCECLASS);
String resourceBundle = scs.getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);
srb = new Resource().getLoader(resourceClass, resourceBundle);
srb = Resource.getResourceLoader(resourceClass, resourceBundle);

if(scs != null) {
defaultListPageSize = scs.getInt("citations.default.list.page.size", DEFAULT_LIST_PAGE_SIZE);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public class FilePickerAction extends PagedResourceHelperAction
private static final String RESOURCEBUNDLE = "resource.bundle.shared";
private String resourceClass = ServerConfigurationService.getString(RESOURCECLASS, DEFAULT_RESOURCECLASS);
private String resourceBundle = ServerConfigurationService.getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);
private ResourceLoader srb = new Resource().getLoader(resourceClass, resourceBundle);
private ResourceLoader srb = Resource.getResourceLoader(resourceClass, resourceBundle);

/** CloudStorage **/
private boolean onedriveOn = ServerConfigurationService.getBoolean(OneDriveService.ONEDRIVE_ENABLED, Boolean.FALSE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ public void setParent(String parent)
private static final String RESOURCEBUNDLE = "resource.bundle.shared";
private final String resourceClass = ServerConfigurationService.getString(RESOURCECLASS, DEFAULT_RESOURCECLASS);
private final String resourceBundle = ServerConfigurationService.getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);
private final ResourceLoader srb = new Resource().getLoader(resourceClass, resourceBundle);
private final ResourceLoader srb = Resource.getResourceLoader(resourceClass, resourceBundle);

static final ResourceConditionsHelper conditionsHelper = new ResourceConditionsHelper();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public SiteEmailNotificationDragAndDrop(String siteId)
private void loadResources(ServerConfigurationService serverConfigurationService) {
resourceClass = serverConfigurationService.getString(RESOURCECLASS, DEFAULT_RESOURCECLASS);
resourceBundle = serverConfigurationService.getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);
rb = new Resource().getLoader(resourceClass, resourceBundle);
rb = Resource.getResourceLoader(resourceClass, resourceBundle);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class FileUploadType extends BaseResourceType
private static final String RESOURCEBUNDLE = "resource.bundle.type";
private String resourceClass = ServerConfigurationService.getString(RESOURCECLASS, DEFAULT_RESOURCECLASS);
private String resourceBundle = ServerConfigurationService.getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);
private ResourceLoader rb = new Resource().getLoader(resourceClass, resourceBundle);
private ResourceLoader rb = Resource.getResourceLoader(resourceClass, resourceBundle);
// private static ResourceLoader rb = new ResourceLoader("types");

private Localizer localizer(final String string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class FolderType extends BaseResourceType implements ExpandableResourceTy
private static final String RESOURCEBUNDLE = "resource.bundle.type";
private String resourceClass = ServerConfigurationService.getString(RESOURCECLASS, DEFAULT_RESOURCECLASS);
private String resourceBundle = ServerConfigurationService.getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);
private ResourceLoader rb = new Resource().getLoader(resourceClass, resourceBundle);
private ResourceLoader rb = Resource.getResourceLoader(resourceClass, resourceBundle);
// private static ResourceLoader rb = new ResourceLoader("types");

protected EnumMap<ActionType, List<ResourceToolAction>> actionMap = new EnumMap<ActionType, List<ResourceToolAction>>(ActionType.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class HtmlDocumentType extends BaseResourceType
private static final String RESOURCEBUNDLE = "resource.bundle.type";
private String resourceClass = ServerConfigurationService.getString(RESOURCECLASS, DEFAULT_RESOURCECLASS);
private String resourceBundle = ServerConfigurationService.getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);
private ResourceLoader rb = new Resource().getLoader(resourceClass, resourceBundle);
private ResourceLoader rb = Resource.getResourceLoader(resourceClass, resourceBundle);
// private static ResourceLoader rb = new ResourceLoader("types");

protected EnumMap<ActionType, List<ResourceToolAction>> actionMap = new EnumMap<ActionType, List<ResourceToolAction>>(ActionType.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public MakeSitePageAction(String id, ActionType actionType, String typeId)
private static final String RESOURCEBUNDLE = "resource.bundle.type";
private String resourceClass = ServerConfigurationService.getString(RESOURCECLASS, DEFAULT_RESOURCECLASS);
private String resourceBundle = ServerConfigurationService.getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);
private ResourceLoader rb = new Resource().getLoader(resourceClass, resourceBundle);
private ResourceLoader rb = Resource.getResourceLoader(resourceClass, resourceBundle);

public boolean available(ContentEntity entity) {;
return ServerConfigurationService.getBoolean("content.make.site.page",true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class TextDocumentType extends BaseResourceType
private static final String RESOURCEBUNDLE = "resource.bundle.type";
private String resourceClass = ServerConfigurationService.getString(RESOURCECLASS, DEFAULT_RESOURCECLASS);
private String resourceBundle = ServerConfigurationService.getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);
private ResourceLoader rb = new Resource().getLoader(resourceClass, resourceBundle);
private ResourceLoader rb = Resource.getResourceLoader(resourceClass, resourceBundle);

protected EnumMap<ActionType, List<ResourceToolAction>> actionMap = new EnumMap<ActionType, List<ResourceToolAction>>(ActionType.class);
protected Map<String, ResourceToolAction> actions = new HashMap<String, ResourceToolAction>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class UrlResourceType extends BaseResourceType
private ServerConfigurationService serverConfigurationService = (ServerConfigurationService) ComponentManager.get("org.sakaiproject.component.api.ServerConfigurationService");;
private String resourceClass = serverConfigurationService.getString(RESOURCECLASS, DEFAULT_RESOURCECLASS);
private String resourceBundle = serverConfigurationService.getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);
private ResourceLoader rb = new Resource().getLoader(resourceClass, resourceBundle);
private ResourceLoader rb = Resource.getResourceLoader(resourceClass, resourceBundle);

// private static ResourceLoader rb = new ResourceLoader("types");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public void init()
// Get resource bundle
String resourceClass = serverConfigurationService().getString(RESOURCECLASS, DEFAULT_RESOURCECLASS);
String resourceBundle = serverConfigurationService().getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);
rb = new Resource().getLoader(resourceClass, resourceBundle);
rb = Resource.getResourceLoader(resourceClass, resourceBundle);

m_relativeAccessPoint = REFERENCE_ROOT;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ public void init()
// Get resource bundle
String resourceClass = m_serverConfigurationService.getString(RESOURCECLASS, DEFAULT_RESOURCECLASS);
String resourceBundle = m_serverConfigurationService.getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);
rb = new Resource().getLoader(resourceClass, resourceBundle);
rb = Resource.getResourceLoader(resourceClass, resourceBundle);

m_relativeAccessPoint = REFERENCE_ROOT;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ public void init()
String imageClassFileBundle = serverConfigurationService.getString(IMAGECLASSFILEBUNDLE, DEFAULT_IMAGECLASSFILEBUNDLE);
String nameFileBundle = serverConfigurationService.getString(NAMEFILEBUNDLE, DEFAULT_NAMEFILEBUNDLE);

m_contentTypeExtensions = new Resource().getLoader(resourceClass, extensionFileBundle);
m_contentTypeImages = new Resource().getLoader(resourceClass, imageFileBundle);
m_contentTypeImageClasses = new Resource().getLoader(resourceClass, imageClassFileBundle);
m_contentTypeDisplayNames = new Resource().getLoader(resourceClass, nameFileBundle);
m_contentTypeExtensions = Resource.getResourceLoader(resourceClass, extensionFileBundle);
m_contentTypeImages = Resource.getResourceLoader(resourceClass, imageFileBundle);
m_contentTypeImageClasses = Resource.getResourceLoader(resourceClass, imageClassFileBundle);
m_contentTypeDisplayNames = Resource.getResourceLoader(resourceClass, nameFileBundle);

// read the content type extensions file, using extension as the key
if (m_contentTypeExtensions != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public DropboxNotification() {
private void loadResources() {
resourceClass = serverConfigurationService.getString(RESOURCECLASS, DEFAULT_RESOURCECLASS);
resourceBundle = serverConfigurationService.getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);
rb = new Resource().getLoader(resourceClass, resourceBundle);
rb = Resource.getResourceLoader(resourceClass, resourceBundle);
}

public DropboxNotification(SecurityService securityService, ContentHostingService contentHostingService, EntityManager entityManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public SiteEmailNotificationContent(SecurityService securityService, ServerConfi
private void loadResources(ServerConfigurationService serverConfigurationService) {
resourceClass = serverConfigurationService.getString(RESOURCECLASS, DEFAULT_RESOURCECLASS);
resourceBundle = serverConfigurationService.getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);
rb = new Resource().getLoader(resourceClass, resourceBundle);
rb = Resource.getResourceLoader(resourceClass, resourceBundle);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ResourceLoader getResourceLoader(String location) {

String resourceClass = getServerConfigurationService().getString(RESOURCECLASS, DEFAULT_RESOURCECLASS);
String resourceBundle = getServerConfigurationService().getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);
ResourceLoader loader = new Resource().getLoader(resourceClass, resourceBundle);
ResourceLoader loader = Resource.getResourceLoader(resourceClass, resourceBundle);
return loader;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ public void init()
// Resource Bundle
String resourceClass = serverConfigurationService.getString(RESOURCECLASS, DEFAULT_RESOURCECLASS);
String resourceBundle = serverConfigurationService.getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);
rb = new Resource().getLoader(resourceClass, resourceBundle);
rb = Resource.getResourceLoader(resourceClass, resourceBundle);

// USE A TIMER INSTEAD OF CREATING A NEW THREAD -AZ
// start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ public void init()
// Get resource bundle
String resourceClass = serverConfigurationService().getString(RESOURCECLASS, DEFAULT_RESOURCECLASS);
String resourceBundle = serverConfigurationService().getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);
rb = new Resource().getLoader(resourceClass, resourceBundle);
rb = Resource.getResourceLoader(resourceClass, resourceBundle);

m_relativeAccessPoint = REFERENCE_ROOT;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ResourceLoader createInstance() {
String resourceBundle = serverConfigurationService.getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);

// Resource starts up the ComponentManager through the cover.
return new Resource().getLoader(resourceClass, resourceBundle);
return Resource.getResourceLoader(resourceClass, resourceBundle);
}

public void setServerConfigurationService(ServerConfigurationService serverConfigurationService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private boolean defaultLowSecurity() {
public ResourceLoader getResourceLoader() {
String resourceClass = serverConfigurationService.getString(RESOURCECLASS, DEFAULT_RESOURCECLASS);
String resourceBundle = serverConfigurationService.getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE);
ResourceLoader loader = new Resource().getLoader(resourceClass, resourceBundle);
ResourceLoader loader = Resource.getResourceLoader(resourceClass, resourceBundle);
return loader;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ public void setUp() throws Exception {
@Test
public void test() {
// KNL-888 Check that multiple calls to getLoader() return the correct loader.
Resource resource = new Resource();
ResourceLoader loader1 = resource.getLoader(ContentHostingService.class.getName(), "bundle1");
ResourceLoader loader2 = resource.getLoader(ContentHostingService.class.getName(), "bundle2");
ResourceLoader loader1 = Resource.getResourceLoader(ContentHostingService.class.getName(), "bundle1");
ResourceLoader loader2 = Resource.getResourceLoader(ContentHostingService.class.getName(), "bundle2");
assertEquals("Bundle 1", loader1.getString("example"));
assertEquals("Bundle 2", loader2.getString("example"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class ZipContentUtil {
private static final String DEFAULT_RESOURCEBUNDLE = "org.sakaiproject.localization.bundle.content.content";
private static final String RESOURCECLASS = "resource.class.content";
private static final String RESOURCEBUNDLE = "resource.bundle.content";
private static ResourceLoader rb = new Resource().getLoader(ServerConfigurationService.getString(RESOURCECLASS, DEFAULT_RESOURCECLASS), ServerConfigurationService.getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE));
private static ResourceLoader rb = Resource.getResourceLoader(ServerConfigurationService.getString(RESOURCECLASS, DEFAULT_RESOURCECLASS), ServerConfigurationService.getString(RESOURCEBUNDLE, DEFAULT_RESOURCEBUNDLE));

public static int getMaxZipExtractFiles() {
if(MAX_ZIP_EXTRACT_FILES == null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,31 @@

package org.sakaiproject.util;

import lombok.extern.slf4j.Slf4j;

import org.sakaiproject.component.cover.ComponentManager;
import org.sakaiproject.util.ResourceLoader;

import lombok.extern.slf4j.Slf4j;

/**
* This class allows a resource bundle to be made accessible though a spring bean.
* This class allows a resource bundle to be made accessible though a spring bean.
* This allows properties to be shared across multiple artifacts without each project having their own
* copy of the bundle.
*
*/
@Slf4j
public class Resource {
public Resource() {
// constructor
}

/**
* Get localized resource bundle via classLoader.
* @param className Java class name providing access to the *.properties file
* @param bundleName default name of bundle.
* @return ResourceLoader or <code>null</code> if the ResourceLoader failed to load.
*/
public ResourceLoader getLoader(String resourceClass, String resourceBundle) {
ResourceLoader loader = null;
try {
loader = new ResourceLoader(resourceBundle, ComponentManager.get(resourceClass).getClass().getClassLoader());
}
catch (Exception e) {
log.warn("WARN: Localized bundle not found: " + e.toString());
}

return loader;
}

/**
* get localized resource bundle via classLoader.
*
* @param className class name providing access to the *.properties file
* @param bundleName default name of bundle.
* @return ResourceLoader or <code>null</code> if the ResourceLoader failed to load.
*/
public static ResourceLoader getResourceLoader(String className, String bundleName) {
try {
return new ResourceLoader(bundleName, ComponentManager.get(className).getClass().getClassLoader());
} catch (Exception e) {
log.debug("Localized bundle {} not found, {}", bundleName, e.toString());
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public class SkinnableCharonPortal extends HttpServlet implements Portal
* messages.
*/
private static ResourceLoader rloader = new ResourceLoader("sitenav");
private static ResourceLoader cmLoader = new Resource().getLoader("org.sakaiproject.portal.api.PortalService", "connection-manager");
private static ResourceLoader cmLoader = Resource.getResourceLoader("org.sakaiproject.portal.api.PortalService", "connection-manager");

/**
* Parameter value to indicate to look up a tool ID within a site
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public ActionReturn getFormattedProfile(EntityReference ref, Map<String, Object>

String currentUserId = getCheckedCurrentUser();

ResourceLoader rl = new Resource().getLoader("org.sakaiproject.portal.api.PortalService", "profile-popup");
ResourceLoader rl = Resource.getResourceLoader("org.sakaiproject.portal.api.PortalService", "profile-popup");

UserProfile userProfile = (UserProfile) profileLogic.getUserProfile(ref.getId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public String getI18nProperties(
if (StringUtils.isNotBlank(resourceClass) && StringUtils.isNotBlank(resourceBundle)) {
try {
// load from spring using SakaiApplicationContext
rb = new Resource().getLoader(resourceClass, resourceBundle);
rb = Resource.getResourceLoader(resourceClass, resourceBundle);
if (rb == null) {
// load from shared lib
rb = new ResourceLoader(resourceBundle, Class.forName(resourceClass).getClassLoader());
Expand Down

0 comments on commit 0531dca

Please sign in to comment.