@Test public void testExtractViewArchive() throws Exception { File viewDir = createNiceMock(File.class); File extractedArchiveDir = createNiceMock(File.class); File viewArchive = createNiceMock(File.class); File archiveDir = createNiceMock(File.class); File entryFile = createNiceMock(File.class); File classesDir = createNiceMock(File.class); File libDir = createNiceMock(File.class); File metaInfDir = createNiceMock(File.class); File fileEntry = createNiceMock(File.class); JarInputStream viewJarFile = createNiceMock(JarInputStream.class); JarEntry jarEntry = createNiceMock(JarEntry.class); InputStream is = createMock(InputStream.class); FileOutputStream fos = createMock(FileOutputStream.class); ViewExtractor viewExtractor = createMock(ViewExtractor.class); ResourceTypeEntity resourceTypeEntity = new ResourceTypeEntity(); resourceTypeEntity.setId(10); resourceTypeEntity.setName("MY_VIEW{1.0.0}"); ViewEntity viewDefinition = ViewEntityTest.getViewEntity(); viewDefinition.setResourceType(resourceTypeEntity); Set<ViewInstanceEntity> viewInstanceEntities = ViewInstanceEntityTest.getViewInstanceEntities(viewDefinition); viewDefinition.setInstances(viewInstanceEntities); Map<File, ViewConfig> viewConfigs = Collections.singletonMap(viewArchive, viewDefinition.getConfiguration()); long resourceId = 99L; for (ViewInstanceEntity viewInstanceEntity : viewInstanceEntities) { ResourceEntity resourceEntity = new ResourceEntity(); resourceEntity.setId(resourceId); resourceEntity.setResourceType(resourceTypeEntity); viewInstanceEntity.setResource(resourceEntity); } Map<String, File> files = new HashMap<String, File>(); if (System.getProperty("os.name").contains("Windows")) { files.put("\\var\\lib\\ambari-server\\resources\\views\\my_view-1.0.0.jar", viewArchive); files.put("\\var\\lib\\ambari-server\\resources\\views\\work", extractedArchiveDir); files.put("\\var\\lib\\ambari-server\\resources\\views\\work\\MY_VIEW{1.0.0}", archiveDir); files.put("\\var\\lib\\ambari-server\\resources\\views\\work\\MY_VIEW{1.0.0}\\view.xml", entryFile); files.put("\\var\\lib\\ambari-server\\resources\\views\\work\\MY_VIEW{1.0.0}\\WEB-INF\\classes", classesDir); files.put("\\var\\lib\\ambari-server\\resources\\views\\work\\MY_VIEW{1.0.0}\\WEB-INF\\lib", libDir); files.put("\\var\\lib\\ambari-server\\resources\\views\\work\\MY_VIEW{1.0.0}\\META-INF", metaInfDir); } else { files.put("/var/lib/ambari-server/resources/views/my_view-1.0.0.jar", viewArchive); files.put("/var/lib/ambari-server/resources/views/work", extractedArchiveDir); files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}", archiveDir); files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/view.xml", entryFile); files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/WEB-INF/classes", classesDir); files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/WEB-INF/lib", libDir); files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/META-INF", metaInfDir); } Map<File, FileOutputStream> outputStreams = new HashMap<File, FileOutputStream>(); outputStreams.put(entryFile, fos); Map<File, JarInputStream> jarFiles = new HashMap<File, JarInputStream>(); jarFiles.put(viewArchive, viewJarFile); expect(configuration.getViewsDir()).andReturn(viewDir); if (System.getProperty("os.name").contains("Windows")) { expect(viewDir.getAbsolutePath()).andReturn("\\var\\lib\\ambari-server\\resources\\views"); } else { expect(viewDir.getAbsolutePath()).andReturn("/var/lib/ambari-server/resources/views"); } expect(configuration.getViewExtractionThreadPoolCoreSize()).andReturn(2).anyTimes(); expect(configuration.getViewExtractionThreadPoolMaxSize()).andReturn(3).anyTimes(); expect(configuration.getViewExtractionThreadPoolTimeout()).andReturn(10000L).anyTimes(); if (System.getProperty("os.name").contains("Windows")) { expect(viewArchive.getAbsolutePath()).andReturn("\\var\\lib\\ambari-server\\resources\\views\\work\\MY_VIEW{1.0.0}").anyTimes(); } else { expect(viewArchive.getAbsolutePath()).andReturn("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}").anyTimes(); } expect(archiveDir.exists()).andReturn(false); if (System.getProperty("os.name").contains("Windows")) { expect(archiveDir.getAbsolutePath()).andReturn("\\var\\lib\\ambari-server\\resources\\views\\work\\MY_VIEW{1.0.0}").anyTimes(); } else { expect(archiveDir.getAbsolutePath()).andReturn("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}").anyTimes(); } Capture<ViewEntity> viewEntityCapture = EasyMock.newCapture(); if (System.getProperty("os.name").contains("Windows")) { expect(viewExtractor.ensureExtractedArchiveDirectory("\\var\\lib\\ambari-server\\resources\\views\\work")).andReturn(true); } else { expect(viewExtractor.ensureExtractedArchiveDirectory("/var/lib/ambari-server/resources/views/work")).andReturn(true); } expect(viewExtractor.extractViewArchive(capture(viewEntityCapture), eq(viewArchive), eq(archiveDir))).andReturn(null); replay(configuration, viewDir, extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir, libDir, metaInfDir, fileEntry, viewJarFile, jarEntry, is, fos, viewExtractor, resourceDAO, viewDAO, viewInstanceDAO); TestViewArchiveUtility archiveUtility = new TestViewArchiveUtility(viewConfigs, files, outputStreams, jarFiles, false); TestViewModule module = new TestViewModule(viewExtractor, archiveUtility, configuration); if (System.getProperty("os.name").contains("Windows")) { Assert.assertTrue(ViewRegistry.extractViewArchive("\\var\\lib\\ambari-server\\resources\\views\\my_view-1.0.0.jar", module, true)); } else { Assert.assertTrue(ViewRegistry.extractViewArchive("/var/lib/ambari-server/resources/views/my_view-1.0.0.jar", module, true)); } verify(configuration, viewDir, extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir, libDir, metaInfDir, fileEntry, viewJarFile, jarEntry, is, fos, viewExtractor, resourceDAO, viewDAO, viewInstanceDAO); }
protected static boolean extractViewArchive(String archivePath, ViewModule viewModule, boolean systemOnly) throws Exception { Injector injector = Guice.createInjector(viewModule); ViewExtractor extractor = injector.getInstance(ViewExtractor.class); ViewArchiveUtility archiveUtility = injector.getInstance(ViewArchiveUtility.class); Configuration configuration = injector.getInstance(Configuration.class); File viewDir = configuration.getViewsDir(); String extractedArchivesPath = viewDir.getAbsolutePath() + File.separator + EXTRACTED_ARCHIVES_DIR; if (extractor.ensureExtractedArchiveDirectory(extractedArchivesPath)) { File archiveFile = archiveUtility.getFile(archivePath); ViewConfig viewConfig = archiveUtility.getViewConfigFromArchive(archiveFile); String commonName = viewConfig.getName(); String version = viewConfig.getVersion(); String viewName = ViewEntity.getViewName(commonName, version); String extractedArchiveDirPath = extractedArchivesPath + File.separator + viewName; File extractedArchiveDirFile = archiveUtility.getFile(extractedArchiveDirPath); if (!extractedArchiveDirFile.exists()) { ViewEntity viewDefinition = new ViewEntity(viewConfig, configuration, extractedArchiveDirPath); if (!systemOnly || viewDefinition.isSystem()) { ClassLoader classLoader = null; try { classLoader = extractor.extractViewArchive(viewDefinition, archiveFile, extractedArchiveDirFile); return true; } finally { if (classLoader instanceof Closeable) { Closeables.closeSilently((Closeable) classLoader); } } } } } return false; }
ViewRegistry { protected static boolean extractViewArchive(String archivePath, ViewModule viewModule, boolean systemOnly) throws Exception { Injector injector = Guice.createInjector(viewModule); ViewExtractor extractor = injector.getInstance(ViewExtractor.class); ViewArchiveUtility archiveUtility = injector.getInstance(ViewArchiveUtility.class); Configuration configuration = injector.getInstance(Configuration.class); File viewDir = configuration.getViewsDir(); String extractedArchivesPath = viewDir.getAbsolutePath() + File.separator + EXTRACTED_ARCHIVES_DIR; if (extractor.ensureExtractedArchiveDirectory(extractedArchivesPath)) { File archiveFile = archiveUtility.getFile(archivePath); ViewConfig viewConfig = archiveUtility.getViewConfigFromArchive(archiveFile); String commonName = viewConfig.getName(); String version = viewConfig.getVersion(); String viewName = ViewEntity.getViewName(commonName, version); String extractedArchiveDirPath = extractedArchivesPath + File.separator + viewName; File extractedArchiveDirFile = archiveUtility.getFile(extractedArchiveDirPath); if (!extractedArchiveDirFile.exists()) { ViewEntity viewDefinition = new ViewEntity(viewConfig, configuration, extractedArchiveDirPath); if (!systemOnly || viewDefinition.isSystem()) { ClassLoader classLoader = null; try { classLoader = extractor.extractViewArchive(viewDefinition, archiveFile, extractedArchiveDirFile); return true; } finally { if (classLoader instanceof Closeable) { Closeables.closeSilently((Closeable) classLoader); } } } } } return false; } }
ViewRegistry { protected static boolean extractViewArchive(String archivePath, ViewModule viewModule, boolean systemOnly) throws Exception { Injector injector = Guice.createInjector(viewModule); ViewExtractor extractor = injector.getInstance(ViewExtractor.class); ViewArchiveUtility archiveUtility = injector.getInstance(ViewArchiveUtility.class); Configuration configuration = injector.getInstance(Configuration.class); File viewDir = configuration.getViewsDir(); String extractedArchivesPath = viewDir.getAbsolutePath() + File.separator + EXTRACTED_ARCHIVES_DIR; if (extractor.ensureExtractedArchiveDirectory(extractedArchivesPath)) { File archiveFile = archiveUtility.getFile(archivePath); ViewConfig viewConfig = archiveUtility.getViewConfigFromArchive(archiveFile); String commonName = viewConfig.getName(); String version = viewConfig.getVersion(); String viewName = ViewEntity.getViewName(commonName, version); String extractedArchiveDirPath = extractedArchivesPath + File.separator + viewName; File extractedArchiveDirFile = archiveUtility.getFile(extractedArchiveDirPath); if (!extractedArchiveDirFile.exists()) { ViewEntity viewDefinition = new ViewEntity(viewConfig, configuration, extractedArchiveDirPath); if (!systemOnly || viewDefinition.isSystem()) { ClassLoader classLoader = null; try { classLoader = extractor.extractViewArchive(viewDefinition, archiveFile, extractedArchiveDirFile); return true; } finally { if (classLoader instanceof Closeable) { Closeables.closeSilently((Closeable) classLoader); } } } } } return false; } @Inject  ViewRegistry(AmbariEventPublisher publisher); }
ViewRegistry { protected static boolean extractViewArchive(String archivePath, ViewModule viewModule, boolean systemOnly) throws Exception { Injector injector = Guice.createInjector(viewModule); ViewExtractor extractor = injector.getInstance(ViewExtractor.class); ViewArchiveUtility archiveUtility = injector.getInstance(ViewArchiveUtility.class); Configuration configuration = injector.getInstance(Configuration.class); File viewDir = configuration.getViewsDir(); String extractedArchivesPath = viewDir.getAbsolutePath() + File.separator + EXTRACTED_ARCHIVES_DIR; if (extractor.ensureExtractedArchiveDirectory(extractedArchivesPath)) { File archiveFile = archiveUtility.getFile(archivePath); ViewConfig viewConfig = archiveUtility.getViewConfigFromArchive(archiveFile); String commonName = viewConfig.getName(); String version = viewConfig.getVersion(); String viewName = ViewEntity.getViewName(commonName, version); String extractedArchiveDirPath = extractedArchivesPath + File.separator + viewName; File extractedArchiveDirFile = archiveUtility.getFile(extractedArchiveDirPath); if (!extractedArchiveDirFile.exists()) { ViewEntity viewDefinition = new ViewEntity(viewConfig, configuration, extractedArchiveDirPath); if (!systemOnly || viewDefinition.isSystem()) { ClassLoader classLoader = null; try { classLoader = extractor.extractViewArchive(viewDefinition, archiveFile, extractedArchiveDirFile); return true; } finally { if (classLoader instanceof Closeable) { Closeables.closeSilently((Closeable) classLoader); } } } } } return false; } @Inject  ViewRegistry(AmbariEventPublisher publisher); static void main(String[] args); Collection<ViewEntity> getDefinitions(); ViewEntity getDefinition(String viewName, String version); ViewEntity getDefinition(ResourceTypeEntity resourceTypeEntity); void addDefinition(ViewEntity definition); Collection<ViewInstanceEntity> getInstanceDefinitions(ViewEntity definition); ViewInstanceEntity getInstanceDefinition(String viewName, String version, String instanceName); void addInstanceDefinition(ViewEntity definition, ViewInstanceEntity instanceDefinition); void removeInstanceDefinition(ViewEntity definition, String instanceName); static void initInstance(ViewRegistry singleton); static ViewRegistry getInstance(); Set<SubResourceDefinition> getSubResourceDefinitions(
      String viewName, String version); void readViewArchives(); void readViewArchives(String viewNameRegExp); boolean instanceExists(ViewInstanceEntity instanceEntity); void installViewInstance(ViewInstanceEntity instanceEntity); void updateViewInstance(ViewInstanceEntity instanceEntity); void updateView(ViewInstanceEntity instanceEntity); ViewInstanceEntity getViewInstanceEntity(String viewName, String instanceName); @Transactional void uninstallViewInstance(ViewInstanceEntity instanceEntity); @Transactional void removeInstanceData(ViewInstanceEntity instanceEntity, String key); @Transactional void copyPrivileges(ViewInstanceEntity sourceInstanceEntity,
                             ViewInstanceEntity targetInstanceEntity); void fireEvent(Event event); synchronized void registerListener(Listener listener, String viewName, String viewVersion); synchronized void unregisterListener(Listener listener, String viewName, String viewVersion); boolean hasPermission(PermissionEntity permissionEntity, ResourceEntity resourceEntity, String userName); boolean checkPermission(String viewName, String version, String instanceName, boolean readOnly); boolean checkPermission(ViewInstanceEntity instanceEntity, boolean readOnly); boolean checkAdmin(); boolean includeDefinition(ViewEntity definitionEntity); void setViewInstanceProperties(ViewInstanceEntity instanceEntity, Map<String, String> properties,
                                        ViewConfig viewConfig, ClassLoader classLoader); Cluster getCluster(ViewInstanceDefinition viewInstance); @Subscribe @AllowConcurrentEvents void onAmbariEvent(ServiceInstalledEvent event); @Subscribe void onClusterConfigFinishedEvent(ClusterConfigFinishedEvent event); void readViewArchive(Path path); }
ViewRegistry { protected static boolean extractViewArchive(String archivePath, ViewModule viewModule, boolean systemOnly) throws Exception { Injector injector = Guice.createInjector(viewModule); ViewExtractor extractor = injector.getInstance(ViewExtractor.class); ViewArchiveUtility archiveUtility = injector.getInstance(ViewArchiveUtility.class); Configuration configuration = injector.getInstance(Configuration.class); File viewDir = configuration.getViewsDir(); String extractedArchivesPath = viewDir.getAbsolutePath() + File.separator + EXTRACTED_ARCHIVES_DIR; if (extractor.ensureExtractedArchiveDirectory(extractedArchivesPath)) { File archiveFile = archiveUtility.getFile(archivePath); ViewConfig viewConfig = archiveUtility.getViewConfigFromArchive(archiveFile); String commonName = viewConfig.getName(); String version = viewConfig.getVersion(); String viewName = ViewEntity.getViewName(commonName, version); String extractedArchiveDirPath = extractedArchivesPath + File.separator + viewName; File extractedArchiveDirFile = archiveUtility.getFile(extractedArchiveDirPath); if (!extractedArchiveDirFile.exists()) { ViewEntity viewDefinition = new ViewEntity(viewConfig, configuration, extractedArchiveDirPath); if (!systemOnly || viewDefinition.isSystem()) { ClassLoader classLoader = null; try { classLoader = extractor.extractViewArchive(viewDefinition, archiveFile, extractedArchiveDirFile); return true; } finally { if (classLoader instanceof Closeable) { Closeables.closeSilently((Closeable) classLoader); } } } } } return false; } @Inject  ViewRegistry(AmbariEventPublisher publisher); static void main(String[] args); Collection<ViewEntity> getDefinitions(); ViewEntity getDefinition(String viewName, String version); ViewEntity getDefinition(ResourceTypeEntity resourceTypeEntity); void addDefinition(ViewEntity definition); Collection<ViewInstanceEntity> getInstanceDefinitions(ViewEntity definition); ViewInstanceEntity getInstanceDefinition(String viewName, String version, String instanceName); void addInstanceDefinition(ViewEntity definition, ViewInstanceEntity instanceDefinition); void removeInstanceDefinition(ViewEntity definition, String instanceName); static void initInstance(ViewRegistry singleton); static ViewRegistry getInstance(); Set<SubResourceDefinition> getSubResourceDefinitions(
      String viewName, String version); void readViewArchives(); void readViewArchives(String viewNameRegExp); boolean instanceExists(ViewInstanceEntity instanceEntity); void installViewInstance(ViewInstanceEntity instanceEntity); void updateViewInstance(ViewInstanceEntity instanceEntity); void updateView(ViewInstanceEntity instanceEntity); ViewInstanceEntity getViewInstanceEntity(String viewName, String instanceName); @Transactional void uninstallViewInstance(ViewInstanceEntity instanceEntity); @Transactional void removeInstanceData(ViewInstanceEntity instanceEntity, String key); @Transactional void copyPrivileges(ViewInstanceEntity sourceInstanceEntity,
                             ViewInstanceEntity targetInstanceEntity); void fireEvent(Event event); synchronized void registerListener(Listener listener, String viewName, String viewVersion); synchronized void unregisterListener(Listener listener, String viewName, String viewVersion); boolean hasPermission(PermissionEntity permissionEntity, ResourceEntity resourceEntity, String userName); boolean checkPermission(String viewName, String version, String instanceName, boolean readOnly); boolean checkPermission(ViewInstanceEntity instanceEntity, boolean readOnly); boolean checkAdmin(); boolean includeDefinition(ViewEntity definitionEntity); void setViewInstanceProperties(ViewInstanceEntity instanceEntity, Map<String, String> properties,
                                        ViewConfig viewConfig, ClassLoader classLoader); Cluster getCluster(ViewInstanceDefinition viewInstance); @Subscribe @AllowConcurrentEvents void onAmbariEvent(ServiceInstalledEvent event); @Subscribe void onClusterConfigFinishedEvent(ClusterConfigFinishedEvent event); void readViewArchive(Path path); static final String API_PREFIX; static final String DEFAULT_AUTO_INSTANCE_URL; }