Search in sources :

Example 1 with MyURLClassLoader

use of net.sourceforge.sqlexplorer.util.MyURLClassLoader in project tdq-studio-se by Talend.

the class FileLabelProvider method createExtraClassPathPanel.

private void createExtraClassPathPanel(final TabFolder tabFolder, TabItem tabItem) {
    Composite parent = new Composite(tabFolder, SWT.NULL);
    parent.setLayout(new FillLayout());
    tabItem.setControl(parent);
    Composite cmp = new Composite(parent, SWT.NULL);
    GridLayout grid = new GridLayout();
    grid.numColumns = 2;
    cmp.setLayout(grid);
    extraClassPathList = new ListViewer(cmp, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    GridData data = new GridData();
    data.grabExcessVerticalSpace = true;
    data.horizontalAlignment = GridData.FILL;
    data.verticalAlignment = GridData.FILL;
    data.grabExcessHorizontalSpace = true;
    extraClassPathList.getControl().setLayoutData(data);
    extraClassPathList.setContentProvider(new FileContentProvider());
    extraClassPathList.setLabelProvider(new FileLabelProvider());
    extraClassPathList.setInput(defaultModel);
    Composite left = new Composite(cmp, SWT.NULL);
    data = new GridData();
    data.horizontalSpan = 1;
    data.grabExcessVerticalSpace = true;
    data.widthHint = 100;
    data.horizontalAlignment = GridData.FILL;
    data.verticalAlignment = GridData.FILL;
    left.setLayoutData(data);
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 1;
    left.setLayout(gridLayout);
    _extraClasspathListDriversBtn = new Button(left, SWT.NULL);
    _extraClasspathListDriversBtn.setText(Messages.getString("List_Drivers_21"));
    _extraClasspathListDriversBtn.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent event) {
            combo.removeAll();
            File file = (File) ((IStructuredSelection) extraClassPathList.getSelection()).getFirstElement();
            if (file != null) {
                try {
                    MyURLClassLoader cl = new MyURLClassLoader(file.toURL());
                    Class[] classes = cl.getAssignableClasses(Driver.class);
                    for (int i = 0; i < classes.length; ++i) {
                        combo.add(classes[i].getName());
                    }
                } catch (Exception ex) {
                    SQLExplorerPlugin.error(ex);
                }
            }
            if (combo.getItemCount() > 0) {
                combo.setText(combo.getItem(0));
            }
            validate();
        }
    });
    data = new GridData();
    data.grabExcessHorizontalSpace = true;
    data.horizontalAlignment = GridData.FILL;
    _extraClasspathListDriversBtn.setLayoutData(data);
    _extraClasspathUpBtn = new Button(left, SWT.NULL);
    _extraClasspathUpBtn.setText(Messages.getString("Up_22"));
    _extraClasspathUpBtn.setEnabled(false);
    data = new GridData();
    data.grabExcessHorizontalSpace = true;
    data.horizontalAlignment = GridData.FILL;
    _extraClasspathUpBtn.setLayoutData(data);
    _extraClasspathDownBtn = new Button(left, SWT.NULL);
    _extraClasspathDownBtn.setText(Messages.getString("Down_23"));
    _extraClasspathDownBtn.setEnabled(false);
    data = new GridData();
    data.grabExcessHorizontalSpace = true;
    data.horizontalAlignment = GridData.FILL;
    _extraClasspathDownBtn.setLayoutData(data);
    newBtn = new Button(left, SWT.NULL);
    newBtn.setText(Messages.getString("New_24"));
    newBtn.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent event) {
            FileDialog dlg = new FileDialog(tabFolder.getShell(), SWT.OPEN);
            dlg.setFilterExtensions(new String[] { "*.jar;*.zip" });
            String str = dlg.open();
            if (str != null) {
                Object obj = new File(str);
                defaultModel.add(obj);
                extraClassPathList.refresh();
                StructuredSelection sel = new StructuredSelection(obj);
                extraClassPathList.setSelection(sel);
            }
        }
    });
    data = new GridData();
    data.grabExcessHorizontalSpace = true;
    data.horizontalAlignment = GridData.FILL;
    newBtn.setLayoutData(data);
    _extraClasspathDeleteBtn = new Button(left, SWT.NULL);
    _extraClasspathDeleteBtn.setText(Messages.getString("Delete_26"));
    _extraClasspathDeleteBtn.setEnabled(false);
    _extraClasspathDeleteBtn.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent event) {
            File f = (File) ((IStructuredSelection) extraClassPathList.getSelection()).getFirstElement();
            if (f != null) {
                defaultModel.remove(f);
                extraClassPathList.refresh();
                if (defaultModel.size() > 0) {
                    Object obj = (defaultModel.toArray())[0];
                    StructuredSelection sel = new StructuredSelection(obj);
                    extraClassPathList.setSelection(sel);
                }
            }
        }
    });
    data = new GridData();
    data.grabExcessHorizontalSpace = true;
    data.horizontalAlignment = GridData.FILL;
    _extraClasspathDeleteBtn.setLayoutData(data);
    extraClassPathList.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            File f = (File) selection.getFirstElement();
            if (f != null) {
                _extraClasspathDeleteBtn.setEnabled(true);
                _extraClasspathListDriversBtn.setEnabled(true);
            } else {
                _extraClasspathListDriversBtn.setEnabled(false);
                _extraClasspathDeleteBtn.setEnabled(false);
            }
        }
    });
}
Also used : ListViewer(org.eclipse.jface.viewers.ListViewer) Composite(org.eclipse.swt.widgets.Composite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) MyURLClassLoader(net.sourceforge.sqlexplorer.util.MyURLClassLoader) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ManagedDriver(net.sourceforge.sqlexplorer.dbproduct.ManagedDriver) Driver(java.sql.Driver) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) FillLayout(org.eclipse.swt.layout.FillLayout) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) File(java.io.File) FileDialog(org.eclipse.swt.widgets.FileDialog)

Example 2 with MyURLClassLoader

use of net.sourceforge.sqlexplorer.util.MyURLClassLoader in project tdq-studio-se by Talend.

the class SqlexplorerService method getDriver.

/*
     * (non-Javadoc)
     * 
     * @see org.talend.dataprofiler.service.ISqlexplorerService#getDriver(java.lang.String, java.lang.String)
     */
@Override
public Driver getDriver(String driverClassName, String jarsPath) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
    Driver driver = null;
    if (StringUtils.isNotEmpty(jarsPath)) {
        try {
            driver = this.createGenericJDBC(jarsPath, driverClassName);
        } catch (Exception e) {
            log.error(e, e);
        }
        return driver;
    }
    SQLExplorerPlugin sqlExplorerPlugin = SQLExplorerPlugin.getDefault();
    if (sqlExplorerPlugin != null) {
        net.sourceforge.sqlexplorer.dbproduct.DriverManager driverModel = sqlExplorerPlugin.getDriverModel();
        try {
            Collection<ManagedDriver> drivers = driverModel.getDrivers();
            for (ManagedDriver managedDriver : drivers) {
                LinkedList<String> jars = managedDriver.getJars();
                List<URL> urls = new ArrayList<URL>();
                for (int i = 0; i < jars.size(); i++) {
                    File file = new File(jars.get(i));
                    if (file.exists()) {
                        urls.add(file.toURI().toURL());
                    }
                }
                if (!urls.isEmpty()) {
                    try {
                        MyURLClassLoader cl;
                        cl = new MyURLClassLoader(urls.toArray(new URL[0]));
                        Class<?> clazz = cl.findClass(driverClassName);
                        if (clazz != null) {
                            driver = (Driver) clazz.newInstance();
                            if (driver != null) {
                                return driver;
                            }
                        }
                    } catch (ClassNotFoundException e) {
                    // do nothings
                    }
                }
            }
        } catch (MalformedURLException e) {
        // do nothings
        }
    }
    if (driver == null) {
        driver = (Driver) Class.forName(driverClassName).newInstance();
    }
    return driver;
}
Also used : MalformedURLException(java.net.MalformedURLException) ManagedDriver(net.sourceforge.sqlexplorer.dbproduct.ManagedDriver) ArrayList(java.util.ArrayList) MyURLClassLoader(net.sourceforge.sqlexplorer.util.MyURLClassLoader) ManagedDriver(net.sourceforge.sqlexplorer.dbproduct.ManagedDriver) Driver(java.sql.Driver) DriverManager(net.sourceforge.sqlexplorer.dbproduct.DriverManager) PartInitException(org.eclipse.ui.PartInitException) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) IFile(org.eclipse.core.resources.IFile) File(java.io.File) SQLExplorerPlugin(net.sourceforge.sqlexplorer.plugin.SQLExplorerPlugin)

Example 3 with MyURLClassLoader

use of net.sourceforge.sqlexplorer.util.MyURLClassLoader in project tdq-studio-se by Talend.

the class FileLabelProvider method createJavaClassPathPanel.

private void createJavaClassPathPanel(TabFolder tabFolder, TabItem tabItem) {
    Composite parent = new Composite(tabFolder, SWT.NULL);
    parent.setLayout(new FillLayout());
    tabItem.setControl(parent);
    Composite cmp = new Composite(parent, SWT.NULL);
    GridLayout grid = new GridLayout();
    grid.numColumns = 2;
    cmp.setLayout(grid);
    javaClassPathList = new ListViewer(cmp, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    GridData data = new GridData();
    data.grabExcessVerticalSpace = true;
    data.horizontalAlignment = GridData.FILL;
    data.verticalAlignment = GridData.FILL;
    data.grabExcessHorizontalSpace = true;
    javaClassPathList.getControl().setLayoutData(data);
    javaClassPathList.setContentProvider(new FileContentProvider());
    javaClassPathList.setLabelProvider(new FileLabelProvider());
    ClassPathListModel model = new ClassPathListModel();
    javaClassPathList.setInput(model);
    Composite left = new Composite(cmp, SWT.NULL);
    data = new GridData();
    data.horizontalSpan = 1;
    data.grabExcessVerticalSpace = true;
    data.widthHint = 100;
    data.horizontalAlignment = GridData.FILL;
    data.verticalAlignment = GridData.FILL;
    left.setLayoutData(data);
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 1;
    left.setLayout(gridLayout);
    _javaClasspathListDriversBtn = new Button(left, SWT.NULL);
    _javaClasspathListDriversBtn.setText(Messages.getString("List_Drivers_20"));
    _javaClasspathListDriversBtn.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent event) {
            combo.removeAll();
            File file = (File) ((IStructuredSelection) javaClassPathList.getSelection()).getFirstElement();
            if (file != null) {
                try {
                    MyURLClassLoader cl = new MyURLClassLoader(file.toURL());
                    Class[] classes = cl.getAssignableClasses(Driver.class);
                    for (int i = 0; i < classes.length; ++i) {
                        combo.add(classes[i].getName());
                    }
                } catch (MalformedURLException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            if (combo.getItemCount() > 0) {
                combo.setText(combo.getItem(0));
            }
        }
    });
    data = new GridData();
    data.grabExcessHorizontalSpace = true;
    data.horizontalAlignment = GridData.FILL;
    _javaClasspathListDriversBtn.setLayoutData(data);
    javaClassPathList.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            File f = (File) selection.getFirstElement();
            if (f != null) {
                if (f.isFile())
                    _javaClasspathListDriversBtn.setEnabled(true);
                else
                    _javaClasspathListDriversBtn.setEnabled(false);
            } else
                _javaClasspathListDriversBtn.setEnabled(false);
        }
    });
    if (model.size() > 0) {
        Object obj = (model.toArray())[0];
        StructuredSelection sel = new StructuredSelection(obj);
        javaClassPathList.setSelection(sel);
    }
}
Also used : ListViewer(org.eclipse.jface.viewers.ListViewer) MalformedURLException(java.net.MalformedURLException) Composite(org.eclipse.swt.widgets.Composite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) MyURLClassLoader(net.sourceforge.sqlexplorer.util.MyURLClassLoader) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ManagedDriver(net.sourceforge.sqlexplorer.dbproduct.ManagedDriver) Driver(java.sql.Driver) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) FillLayout(org.eclipse.swt.layout.FillLayout) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IOException(java.io.IOException) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) File(java.io.File)

Aggregations

File (java.io.File)3 MalformedURLException (java.net.MalformedURLException)3 Driver (java.sql.Driver)3 ManagedDriver (net.sourceforge.sqlexplorer.dbproduct.ManagedDriver)3 MyURLClassLoader (net.sourceforge.sqlexplorer.util.MyURLClassLoader)3 IOException (java.io.IOException)2 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 ListViewer (org.eclipse.jface.viewers.ListViewer)2 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)2 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 FillLayout (org.eclipse.swt.layout.FillLayout)2 GridData (org.eclipse.swt.layout.GridData)2 GridLayout (org.eclipse.swt.layout.GridLayout)2 Button (org.eclipse.swt.widgets.Button)2 Composite (org.eclipse.swt.widgets.Composite)2 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1