Skip to content

Commit

Permalink
ViewsRepository: load factories in ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Nov 9, 2017
1 parent 79d5fc8 commit 54e5c85
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/main/java/de/metas/ui/web/view/ViewsRepository.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package de.metas.ui.web.view;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
Expand All @@ -24,6 +26,7 @@

import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Streams;

import de.metas.logging.LogManager;
Expand Down Expand Up @@ -67,7 +70,7 @@ public class ViewsRepository implements IViewsRepository
{
private static final Logger logger = LogManager.getLogger(ViewsRepository.class);

private final ConcurrentHashMap<ArrayKey, IViewFactory> factories = new ConcurrentHashMap<>();
private final ImmutableMap<ArrayKey, IViewFactory> factories;
@Autowired
private SqlViewFactory defaultFactory;

Expand All @@ -85,8 +88,12 @@ public class ViewsRepository implements IViewsRepository
* @param neededForDBAccess not used in here, but we need to cause spring to initialize it <b>before</b> this component can be initialized.
* So, if you clean this up, please make sure that the webui-API still starts up ^^.
*/
public ViewsRepository(@NonNull final Adempiere neededForDBAccess)
public ViewsRepository(
@NonNull final Adempiere neededForDBAccess,
@NonNull final Collection<IViewFactory> viewFactories)
{
factories = createFactoriesMap(viewFactories);
logger.info("Registered following view factories: ", factories);
}

@PostConstruct
Expand Down Expand Up @@ -117,15 +124,16 @@ private static void truncateTable(final String tableName)
}
}

@Autowired
private void registerAnnotatedFactories(final Collection<IViewFactory> viewFactories)
private static ImmutableMap<ArrayKey, IViewFactory> createFactoriesMap(final Collection<IViewFactory> viewFactories)
{
final Map<ArrayKey, IViewFactory> factories = new HashMap<>();
for (final IViewFactory factory : viewFactories)
{
final ViewFactory annotation = factory.getClass().getAnnotation(ViewFactory.class);
if (annotation == null)
{
logger.info("Skip registering {} because it's not annotated with {}", factory, ViewFactory.class);
// this might be a development bug
logger.warn("Skip {} because it's not annotated with {}", factory, ViewFactory.class);
continue;
}

Expand All @@ -139,9 +147,10 @@ private void registerAnnotatedFactories(final Collection<IViewFactory> viewFacto
for (final JSONViewDataType viewType : viewTypes)
{
factories.put(mkFactoryKey(windowId, viewType), factory);
logger.info("Registered {} for windowId={}, viewType={}", factory, windowId, viewTypes);
}
}

return ImmutableMap.copyOf(factories);
}

private final IViewFactory getFactory(final WindowId windowId, final JSONViewDataType viewType)
Expand Down

0 comments on commit 54e5c85

Please sign in to comment.