Java Code Examples for org.openide.util.lookup.Lookups#executeWith()

The following examples show how to use org.openide.util.lookup.Lookups#executeWith() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestProcessorLookupGetDefaultTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testChangeOfDefaultLookupAppliedToRPTask() throws Exception {
    Lookup prev = Lookup.getDefault();
    final Lookup my = new AbstractLookup(new InstanceContent());
    final Thread myThread = Thread.currentThread();
    final RequestProcessor.Task[] task = { null };
    final boolean[] ok = { false };
    
    Lookups.executeWith(my, new Runnable() {
        @Override
        public void run() {
            assertSame("Default lookup has been changed", my, Lookup.getDefault());

            if (task[0] == null) {
                assertSame("We are being executed in the same thread", myThread, Thread.currentThread());
                // once again in the RP
                task[0] = RequestProcessor.getDefault().post(this, 500);
            } else {
                ok[0] = true;
            }
        }
    });
    assertNotNull("In my lookup code executed OK", task[0]);
    assertEquals("Current lookup back to normal", prev, Lookup.getDefault());
    task[0].waitFinished();
    assertTrue("Even RP task had the right lookup", ok[0]);
}
 
Example 2
Source File: DerbyOptionsTest2.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testDerbyLocationIsNotNullWhenBundledDerbyInstalled() throws Exception {
    // create a fake bundled derby database installation
    File bundledDerby = new File(userdir, DerbyOptions.INST_DIR);
    createFakeDerbyInstallation(bundledDerby);

    // assert the bundled derby is installed
    Lookups.executeWith(Lookups.singleton(new InstalledFileLocatorImpl(userdir)),
            new Runnable() {

                @Override
                public void run() {
                    String derbyLocation = DerbyOptions.getDefaultInstallLocation();
                    assertNotNull(derbyLocation);

                    DerbyOptions.getDefault().setLocation(externalDerby.getAbsolutePath());
                    assertFalse(DerbyOptions.getDefault().isLocationNull());

                    DerbyOptions.getDefault().setLocation(""); // this should set the location to the one of the bundled derby
                    assertFalse(DerbyOptions.getDefault().isLocationNull());
                    assertEquals(DerbyOptions.getDefault().getLocation(), derbyLocation);
                }
            });

}
 
Example 3
Source File: DerbyDatabasesTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void setUp() throws Exception {
    super.setUp();
    
    clearWorkDir();

    systemHome = new File(getWorkDir(), ".netbeans-derby");
    systemHome.mkdirs();
    
    Lookups.executeWith(sampleDBLookup, new Runnable() {
        @Override
        public void run() {
            DerbyOptions.getDefault().setSystemHome(systemHome.getAbsolutePath());
            try {
                JDBCDriverManager.getDefault().addDriver(JDBCDriver.create(DerbyOptions.DRIVER_DISP_NAME_NET, DerbyOptions.DRIVER_DISP_NAME_NET, DerbyOptions.DRIVER_CLASS_NET, new URL[] {}));
            } catch (DatabaseException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    });
    
    
}
 
Example 4
Source File: DerbyDatabasesTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testExtractSampleDatabase() throws Exception {
    Lookups.executeWith(sampleDBLookup, new Runnable() {

        @Override
        public void run() {
            try {
                DerbyDatabasesImpl.getDefault().extractSampleDatabase("newdb", false);
                File newDBDir = new File(systemHome, "newdb");
                Set sampleDBFiles = new HashSet(Arrays.asList(newDBDir.list()));

                assertEquals(4, sampleDBFiles.size());
                assertTrue(sampleDBFiles.contains("log"));
                assertTrue(sampleDBFiles.contains("seg0"));
                assertTrue(sampleDBFiles.contains("service.properties"));
                assertTrue(sampleDBFiles.contains("README_DO_NOT_TOUCH_FILES.txt"));
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    });
}
 
Example 5
Source File: DerbyDatabasesTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testDatabaseNotExtractedToExistingDirectoryIssue80122() {
    final boolean[] exceptionHappend = new boolean[1];
    
    Lookups.executeWith(sampleDBLookup, new Runnable() {

        @Override
        public void run() {
            try {
                File sampleDir = new File(systemHome, "sample");
                sampleDir.mkdirs();
                
                FileUtil.toFileObject(sampleDir).createData("test.file");

                assertEquals("There should be no files in the sample directory", 1, sampleDir.listFiles().length);

                DerbyDatabasesImpl.getDefault().extractSampleDatabase("sample", false);
            } catch (IOException ex) {
                exceptionHappend[0] = true;
            }
        }
    });
    
    assertTrue("Extracting sample db was interrupted", exceptionHappend[0]);
}
 
Example 6
Source File: DerbyDatabasesTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testDatabaseNotExtractedIfDBExists() {
    final boolean[] exceptionHappend = new boolean[1];
    
    Lookups.executeWith(sampleDBLookup, new Runnable() {

        @Override
        public void run() {
            try {
                DerbyDatabasesImpl.getDefault().extractSampleDatabase("sample1", true);
                DerbyDatabasesImpl.getDefault().extractSampleDatabase("sample1", true);
            } catch (IOException ex) {
                exceptionHappend[0] = true;
            }
        }
    });
    
    assertTrue("Extracting sample db was interrupted", exceptionHappend[0]);
}
 
Example 7
Source File: DerbyDatabasesTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testDatabaseSilentlyNotExtractedIfExists() {
    final boolean[] exceptionHappend = new boolean[1];
    
    Lookups.executeWith(sampleDBLookup, new Runnable() {

        @Override
        public void run() {
            try {
                DerbyDatabasesImpl.getDefault().extractSampleDatabase("sample2", false);
                DerbyDatabasesImpl.getDefault().extractSampleDatabase("sample2", false);
            } catch (IOException ex) {
                exceptionHappend[0] = true;
            }
        }
    });
    
    assertFalse("Extracting sample db was not interrupted", exceptionHappend[0]);
}
 
Example 8
Source File: DerbyOptionsTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testDerbyLocationIsNullWhenBundledDerbyNotInstalled() throws IOException {
    // assert the bundled derby is installed
    Lookups.executeWith(Lookups.singleton(new InstalledFileLocatorImpl(userdir)),
            new Runnable() {

                @Override
                public void run() {
                    final File bundledDerby = new File(userdir, DerbyOptions.INST_DIR);
                    if (bundledDerby.exists()) {
                        try {
                            FileUtil.toFileObject(bundledDerby).delete();
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }
                    }

                    assertNull(DerbyOptions.getDefaultInstallLocation());

                    DerbyOptions.getDefault().setLocation(externalDerby.getAbsolutePath());
                    assertFalse(DerbyOptions.getDefault().isLocationNull());

                    DerbyOptions.getDefault().setLocation("");
                    assertTrue(DerbyOptions.getDefault().isLocationNull());
                }
            });
}
 
Example 9
Source File: TestBase.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void setUp() throws Exception {
    super.setUp();
   
    clearWorkDir();
    
    DerbyDatabasesTest.SampleDatabaseLocator sdl = new DerbyDatabasesTest.SampleDatabaseLocator();
    sampleDBLookup = new ProxyLookup(Lookup.getDefault(), Lookups.singleton(sdl));
    
    Lookups.executeWith(sampleDBLookup, new Runnable() {
        @Override
        public void run() {
            // Force initialization of JDBCDrivers
            JDBCDriverManager jdm = JDBCDriverManager.getDefault();
            JDBCDriver[] registeredDrivers = jdm.getDrivers();
        }
    });
}
 
Example 10
Source File: RunWhenScanFinishedSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static void performDeferredTasks() {
    DeferredTask[] _todo;
    synchronized (todo) {
        _todo = todo.toArray(new DeferredTask[todo.size()]);
        todo.clear();
    }
    for (final DeferredTask rq : _todo) {
        Lookups.executeWith(
            rq.context,
            new Runnable() {
                @Override
                public void run() {
                    try {
                        TaskProcessor.runUserTask(rq.task, rq.sources);
                    } catch (ParseException e) {
                        Exceptions.printStackTrace(e);
                    } finally {
                        rq.sync.taskFinished();
                    }
                }
            });
    }
}
 
Example 11
Source File: RunWhenScanFinishedSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static void performScan (
        @NonNull final Runnable runnable,
        @NonNull final Lookup context) {
    lock.writeLock().lock();
    try {
        LOG.log(
                Level.FINE,
                "performScan:entry",    //NOI18N
                runnable);
        Lookups.executeWith(context, runnable);
        LOG.log(
                Level.FINE,
                "performScan:exit",     //NOI18N
                runnable);
    } finally {
        lock.writeLock().unlock();
    }
}
 
Example 12
Source File: MimePathLookup.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Creates a new instance of MimePathLookup */
public MimePathLookup(MimePath mimePath) {
    super();

    if (mimePath == null) {
        throw new NullPointerException("Mime path can't be null."); //NOI18N
    }

    this.mimePath = mimePath;
    this.mimePathBanned = mimePath.size() > 0 && mimePath.getMimeType(0).contains("text/base"); //NOI18N
    
    class R implements Runnable {
        Lookup.Result<MimeDataProvider> dataProviders;
        Lookup.Result<MimeLookupInitializer> mimeInitializers;
        
        public void run() {
            dataProviders = Lookup.getDefault().lookup(new Lookup.Template<MimeDataProvider>(MimeDataProvider.class));
            dataProviders.addLookupListener(WeakListeners.create(LookupListener.class, MimePathLookup.this, dataProviders));

            mimeInitializers = Lookup.getDefault().lookup(new Lookup.Template<MimeLookupInitializer>(MimeLookupInitializer.class));
            mimeInitializers.addLookupListener(WeakListeners.create(LookupListener.class, MimePathLookup.this, mimeInitializers));
        }
    }
    R r = new R();
    Lookups.executeWith(null, r);
    this.dataProviders = r.dataProviders;
    this.mimeInitializers = r.mimeInitializers;
}
 
Example 13
Source File: FolderPathLookup.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Collection<? extends CustomInstanceFactory> getInstanceFactories() {
    Lookup.Result<CustomInstanceFactory> v = factories;
    if (v != null) {
        return v.allInstances();
    }
    final Lookup.Result<CustomInstanceFactory>[] fr = new Lookup.Result[1];
    // ensure the system - global Lookup is used
    Lookups.executeWith(null, new Runnable() {
        public void run() {
            fr[0] = factories = Lookup.getDefault().lookupResult(CustomInstanceFactory.class);
        }
    });
    return fr[0].allInstances();
}
 
Example 14
Source File: ProviderRegistryTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testDynamicGetProviders () throws Exception {
    UnitTestUtils.prepareTest(new String [] { "/org/netbeans/modules/navigator/resources/testGetProvidersLayer.xml" });
    FileObject root = FileUtil.createMemoryFileSystem().getRoot();
    FileObject file1 = root.createData("1");
    FileObject file2 = root.createData("2");
    Lookup checkingProvider = Lookups.singleton(new DynamicRegistration() {
        @Override
        public Collection<? extends NavigatorPanel> panelsFor(URI file) {
            return file1.toURI().equals(file) ? Collections.singletonList(new MarvelousDataTypeProvider())
                                              : Collections.emptyList();
        }
    });
    Lookups.executeWith(new ProxyLookup(Lookup.getDefault(), checkingProvider), () -> {
        ProviderRegistry providerReg = ProviderRegistry.getInstance();

        System.out.println("Asking for masked out file...");
        assertEquals(0, providerReg.getProviders("image/non_existent_type", file2).size());

        System.out.println("Asking for valid file...");
        Collection<? extends NavigatorPanel> result = providerReg.getProviders("image/non_existent_type", file1);
        assertEquals(1, result.size());
        NavigatorPanel np = result.iterator().next();
        assertTrue(np instanceof MarvelousDataTypeProvider);
        MarvelousDataTypeProvider provider = (MarvelousDataTypeProvider)np;
        assertEquals(MARVELOUS_DATA_TYPE_NAME, provider.getDisplayName());
    });
}
 
Example 15
Source File: TaskProcessor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void execute() throws Exception {
    if (!owner.compareAndSet(null, Thread.currentThread())) {
        throw new IllegalStateException("Already running");    //NOI18N
    }
    try {
       Lookups.executeWith(r.context, this);
    } catch (Transfer t) {
        throw (Exception) t.getCause();
    } finally {
        owner.set(null);
    }
}