Search in sources :

Example 41 with Bundle

use of org.osgi.framework.Bundle in project tdi-studio-se by Talend.

the class StandAloneTalendJavaEditor method init.

@Override
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
    super.init(site, input);
    Bundle bundle = FrameworkUtil.getBundle(StandAloneTalendJavaEditor.class);
    lockService = bundle.getBundleContext().registerService(EventHandler.class.getName(), new EventHandler() {

        @Override
        public void handleEvent(Event event) {
            String lockTopic = Constant.REPOSITORY_ITEM_EVENT_PREFIX + Constant.ITEM_LOCK_EVENT_SUFFIX;
            if (lockTopic.equals(event.getTopic())) {
                if (!isEditable) {
                    Object o = event.getProperty(Constant.ITEM_EVENT_PROPERTY_KEY);
                    if (o instanceof FileItem) {
                        boolean isTheCorrectEditor = false;
                        Property property = ((FileItem) o).getProperty();
                        if (property != null) {
                            String eventItemId = property.getId();
                            //$NON-NLS-1$
                            String currentOpenedItemId = "";
                            if (rEditorInput != null) {
                                currentOpenedItemId = rEditorInput.getId();
                            }
                            isTheCorrectEditor = currentOpenedItemId.equals(eventItemId);
                        }
                        if (!isTheCorrectEditor) {
                            return;
                        }
                        item.getProperty().eAdapters().remove(dirtyListener);
                        item = (FileItem) o;
                        item.getProperty().eAdapters().add(dirtyListener);
                        if (isEditable()) {
                            isEditable = true;
                            rEditorInput.getFile().setReadOnly(false);
                            getSite().getShell().getDisplay().asyncExec(new Runnable() {

                                @Override
                                public void run() {
                                    setFocus();
                                    ISourceViewer viewer = getViewer();
                                    if (viewer != null) {
                                        StyledText styledText = viewer.getTextWidget();
                                        if (styledText != null) {
                                            styledText.setBackground(bgColorForEditabeItem);
                                            styledText.setDragDetect(true);
                                        }
                                    }
                                }
                            });
                            try {
                                ICodeGeneratorService service = (ICodeGeneratorService) GlobalServiceRegister.getDefault().getService(ICodeGeneratorService.class);
                                if (o instanceof RoutineItem) {
                                    ITalendSynchronizer routineSynchronizer = service.createJavaRoutineSynchronizer();
                                    routineSynchronizer.syncRoutine((RoutineItem) o, true);
                                } else if (o instanceof SQLPatternItem) {
                                    ISQLPatternSynchronizer sqlPatternSynchronizer = service.getSQLPatternSynchronizer();
                                    sqlPatternSynchronizer.syncSQLPattern((SQLPatternItem) o, true);
                                } else {
                                    //$NON-NLS-1$
                                    org.talend.commons.exception.ExceptionHandler.process(new Exception("Uncatched case"));
                                }
                                setName();
                            } catch (Exception e) {
                                org.talend.commons.exception.ExceptionHandler.process(e);
                            }
                        }
                    }
                }
            }
        }
    }, new Hashtable<String, String>(//$NON-NLS-1$
    Collections.singletonMap(EventConstants.EVENT_TOPIC, Constant.REPOSITORY_ITEM_EVENT_PREFIX + "*")));
}
Also used : ITalendSynchronizer(org.talend.designer.codegen.ITalendSynchronizer) StyledText(org.eclipse.swt.custom.StyledText) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) ISQLPatternSynchronizer(org.talend.designer.codegen.ISQLPatternSynchronizer) EventHandler(org.osgi.service.event.EventHandler) RoutineItem(org.talend.core.model.properties.RoutineItem) SQLPatternItem(org.talend.core.model.properties.SQLPatternItem) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) LoginException(org.talend.commons.exception.LoginException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistenceException(org.talend.commons.exception.PersistenceException) ICodeGeneratorService(org.talend.designer.codegen.ICodeGeneratorService) FileItem(org.talend.core.model.properties.FileItem) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IResourceChangeEvent(org.eclipse.core.resources.IResourceChangeEvent) ResourceChangeEvent(org.eclipse.core.internal.events.ResourceChangeEvent) Event(org.osgi.service.event.Event) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) Property(org.talend.core.model.properties.Property)

Example 42 with Bundle

use of org.osgi.framework.Bundle in project tdi-studio-se by Talend.

the class ComponentsFactory method getComponentsLocation.

private File getComponentsLocation(String folder, AbstractComponentsProvider provider) {
    File file = null;
    try {
        if (provider != null) {
            file = provider.getInstallationFolder();
        } else {
            String componentsPath = IComponentsFactory.COMPONENTS_LOCATION;
            Bundle b = Platform.getBundle(componentsPath);
            IBrandingService breaningService = (IBrandingService) GlobalServiceRegister.getDefault().getService(IBrandingService.class);
            if (breaningService.isPoweredOnlyCamel()) {
                componentsPath = IComponentsFactory.CAMEL_COMPONENTS_LOCATION;
            }
            URL url = FileLocator.find(b, new Path(folder), null);
            if (url == null) {
                return null;
            }
            URL fileUrl = FileLocator.toFileURL(url);
            file = new File(fileUrl.getPath());
        }
    } catch (Exception e) {
        ExceptionHandler.process(e);
    }
    return file;
}
Also used : Path(org.eclipse.core.runtime.Path) ComponentBundleToPath(org.talend.designer.core.model.components.ComponentBundleToPath) Bundle(org.osgi.framework.Bundle) ResourceBundle(java.util.ResourceBundle) IBrandingService(org.talend.core.ui.branding.IBrandingService) File(java.io.File) URL(java.net.URL) FileNotFoundException(java.io.FileNotFoundException) BusinessException(org.talend.commons.exception.BusinessException) IOException(java.io.IOException)

Example 43 with Bundle

use of org.osgi.framework.Bundle in project tdi-studio-se by Talend.

the class ComponentsFactory method loadComponentsFromFolder.

private void loadComponentsFromFolder(String pathSource, AbstractComponentsProvider provider) {
    boolean isCustom = false;
    if ("org.talend.designer.components.model.UserComponentsProvider".equals(provider.getId()) || "org.talend.designer.components.exchange.ExchangeComponentsProvider".equals(provider.getId())) {
        isCustom = true;
    }
    File source;
    try {
        source = provider.getInstallationFolder();
    } catch (IOException e1) {
        ExceptionHandler.process(e1);
        return;
    }
    File[] childDirectories;
    FileFilter fileFilter = new FileFilter() {

        @Override
        public boolean accept(final File file) {
            return file.isDirectory() && file.getName().charAt(0) != '.' && !file.getName().equals(IComponentsFactory.EXTERNAL_COMPONENTS_INNER_FOLDER);
        }
    };
    if (source == null) {
        //$NON-NLS-1$
        ExceptionHandler.process(new Exception(Messages.getString("ComponentsFactory.componentNotFound") + pathSource));
        return;
    }
    childDirectories = source.listFiles(fileFilter);
    IBrandingService service = (IBrandingService) GlobalServiceRegister.getDefault().getService(IBrandingService.class);
    // String[] availableComponents = service.getBrandingConfiguration().getAvailableComponents();
    FileFilter skeletonFilter = new FileFilter() {

        @Override
        public boolean accept(final File file) {
            String fileName = file.getName();
            return file.isFile() && fileName.charAt(0) != '.' && (fileName.endsWith(SKELETON_SUFFIX) || fileName.endsWith(INCLUDEFILEINJET_SUFFIX));
        }
    };
    // Changed by Marvin Wang on Feb.22 for bug TDI-19166, caz the test ConnectionManagerTest maybe get the null
    // context.
    BundleContext context = null;
    if (Platform.getProduct() != null) {
        final Bundle definingBundle = Platform.getProduct().getDefiningBundle();
        if (definingBundle != null) {
            context = definingBundle.getBundleContext();
        }
    }
    if (context == null) {
        context = CodeGeneratorActivator.getDefault().getBundle().getBundleContext();
    }
    ServiceReference sref = context.getServiceReference(PackageAdmin.class.getName());
    PackageAdmin admin = (PackageAdmin) context.getService(sref);
    String bundleName;
    if (!isCustom) {
        bundleName = admin.getBundle(provider.getClass()).getSymbolicName();
    } else {
        bundleName = IComponentsFactory.COMPONENTS_LOCATION;
    }
    if (childDirectories != null) {
        if (monitor != null) {
            this.subMonitor = SubMonitor.convert(monitor, Messages.getString("ComponentsFactory.load.components"), //$NON-NLS-1$
            childDirectories.length);
        }
        if (skeletonList != null) {
            // to optimize the size of the array
            skeletonList.ensureCapacity(childDirectories.length);
            for (File currentFolder : childDirectories) {
                // get the skeleton files first, then XML config files later.
                File[] skeletonFiles = currentFolder.listFiles(skeletonFilter);
                if (skeletonFiles != null) {
                    for (File file : skeletonFiles) {
                        // path
                        skeletonList.add(file.getAbsolutePath());
                    }
                }
                try {
                    File xmlMainFile = new File(currentFolder, ComponentFilesNaming.getInstance().getMainXMLFileName(currentFolder.getName(), getCodeLanguageSuffix()));
                    if (!xmlMainFile.exists()) {
                        // if not a component folder, ignore it.
                        continue;
                    }
                    String currentXmlSha1 = null;
                    try {
                        currentXmlSha1 = SHA1Util.calculateFromTextStream(new FileInputStream(xmlMainFile));
                    } catch (FileNotFoundException e) {
                    // nothing since exceptions are directly in the check bellow
                    }
                    // Need to check if this component is already in the cache or not.
                    // if yes, then we compare the sha1... and if different we reload the component
                    // if component is not in the cache, of course just load it!
                    ComponentsCache cache = ComponentManager.getComponentCache();
                    boolean foundComponentIsSame = false;
                    ComponentInfo existingComponentInfoInCache = null;
                    if (cache.getComponentEntryMap().containsKey(currentFolder.getName())) {
                        EList<ComponentInfo> infos = cache.getComponentEntryMap().get(currentFolder.getName());
                        for (ComponentInfo info : infos) {
                            if (StringUtils.equals(bundleName, info.getSourceBundleName())) {
                                existingComponentInfoInCache = info;
                                if (StringUtils.equals(info.getSha1(), currentXmlSha1)) {
                                    foundComponentIsSame = true;
                                }
                                // found component, no matter changed or not
                                break;
                            }
                        }
                    }
                    if (foundComponentIsSame) {
                        // it should go here mainly for commandline or if use like ctrl+shift+f3
                        if (componentsCache.containsKey(xmlMainFile.getAbsolutePath())) {
                            IComponent componentFromThisProvider = null;
                            for (IComponent component : componentsCache.get(xmlMainFile.getAbsolutePath()).values()) {
                                if (component instanceof EmfComponent) {
                                    if (bundleName.equals(((EmfComponent) component).getSourceBundleName())) {
                                        componentFromThisProvider = component;
                                        break;
                                    }
                                }
                            }
                            if (componentFromThisProvider != null) {
                                // In headless mode, we assume the components won't change and we will use a cache
                                componentList.add(componentFromThisProvider);
                                if (isCustom) {
                                    customComponentList.add(componentFromThisProvider);
                                }
                                continue;
                            }
                        }
                    }
                    if (!foundComponentIsSame) {
                        ComponentFileChecker.checkComponentFolder(currentFolder, getCodeLanguageSuffix());
                    }
                    String pathName = xmlMainFile.getAbsolutePath();
                    String applicationPath = ComponentBundleToPath.getPathFromBundle(bundleName);
                    // pathName = C:\myapp\plugins\myplugin\components\mycomponent\mycomponent.xml
                    pathName = (new Path(pathName)).toPortableString();
                    // pathName = C:/myapp/plugins/myplugin/components/mycomponent/mycomponent.xml
                    //$NON-NLS-1$
                    pathName = pathName.replace(applicationPath, "");
                    // pathName = /components/mycomponent/mycomponent.xml
                    // if not already in memory, just load the component from cache.
                    // if the component is already existing in cache and if it's the same, it won't reload all (cf
                    // flag: foundComponentIsSame)
                    EmfComponent currentComp = new EmfComponent(pathName, bundleName, xmlMainFile.getParentFile().getName(), pathSource, cache, foundComponentIsSame, provider);
                    if (!foundComponentIsSame) {
                        // force to call some functions to update the cache. (to improve)
                        currentComp.isVisibleInComponentDefinition();
                        currentComp.isTechnical();
                        currentComp.getOriginalFamilyName();
                        currentComp.getTranslatedFamilyName();
                        currentComp.getPluginExtension();
                        currentComp.getVersion();
                        currentComp.getModulesNeeded(null);
                        currentComp.getPluginDependencies();
                        // end of force cache update.
                        EList<ComponentInfo> componentsInfo = cache.getComponentEntryMap().get(currentFolder.getName());
                        for (ComponentInfo cInfo : componentsInfo) {
                            if (cInfo.getSourceBundleName().equals(bundleName)) {
                                cInfo.setSha1(currentXmlSha1);
                                break;
                            }
                        }
                        // this will force to save the cache later.
                        ComponentManager.setModified(true);
                    }
                    boolean hiddenComponent = false;
                    Collection<IComponentFactoryFilter> filters = ComponentsFactoryProviderManager.getInstance().getProviders();
                    for (IComponentFactoryFilter filter : filters) {
                        if (!filter.isAvailable(currentComp.getName())) {
                            hiddenComponent = true;
                            break;
                        }
                    }
                    // just don't load it
                    if (hiddenComponent && !(currentComp.getOriginalFamilyName().contains("Technical") || currentComp.isTechnical())) {
                        continue;
                    }
                    componentToProviderMap.put(currentComp, provider);
                    // hide it
                    if (hiddenComponent && (currentComp.getOriginalFamilyName().contains("Technical") || currentComp.isTechnical())) {
                        currentComp.setVisible(false);
                        currentComp.setTechnical(true);
                    }
                    if (provider.getId().contains("Camel")) {
                        currentComp.setPaletteType(ComponentCategory.CATEGORY_4_CAMEL.getName());
                    } else {
                        currentComp.setPaletteType(currentComp.getType());
                    }
                    if (componentList.contains(currentComp)) {
                        //$NON-NLS-1$ //$NON-NLS-2$
                        log.warn("Component " + currentComp.getName() + " already exists. Cannot load user version.");
                    } else {
                        // currentComp.setResourceBundle(getComponentResourceBundle(currentComp, source.toString(),
                        // null,
                        // provider));
                        currentComp.setProvider(provider);
                        componentList.add(currentComp);
                        if (isCustom) {
                            customComponentList.add(currentComp);
                        }
                        if (pathSource != null) {
                            Path userComponent = new Path(pathSource);
                            Path templatePath = new Path(IComponentsFactory.COMPONENTS_INNER_FOLDER + File.separatorChar + IComponentsFactory.EXTERNAL_COMPONENTS_INNER_FOLDER + File.separatorChar + ComponentUtilities.getExtFolder(OLD_COMPONENTS_USER_INNER_FOLDER));
                            if (userComponent.equals(templatePath)) {
                                userComponentList.add(currentComp);
                            }
                        }
                    }
                    // componentsCache only used bellow in case of headless (commandline) or if use like
                    // ctrl+shift+f3
                    String componentName = xmlMainFile.getAbsolutePath();
                    if (!componentsCache.containsKey(componentName)) {
                        componentsCache.put(componentName, new HashMap<String, IComponent>());
                    }
                    componentsCache.get(xmlMainFile.getAbsolutePath()).put(currentComp.getPaletteType(), currentComp);
                } catch (MissingMainXMLComponentFileException e) {
                    //$NON-NLS-1$ //$NON-NLS-2$
                    log.trace(currentFolder.getName() + " is not a " + getCodeLanguageSuffix() + " component", e);
                } catch (BusinessException e) {
                    BusinessException ex = new BusinessException(//$NON-NLS-1$ //$NON-NLS-2$
                    "Cannot load component \"" + currentFolder.getName() + "\": " + e.getMessage(), e);
                    ExceptionHandler.process(ex, Level.ERROR);
                }
                if (this.subMonitor != null) {
                    this.subMonitor.worked(1);
                }
                if (this.monitor != null && this.monitor.isCanceled()) {
                    return;
                }
            }
            // to optimize the size of the array
            skeletonList.trimToSize();
        }
    }
}
Also used : IComponent(org.talend.core.model.components.IComponent) IComponentFactoryFilter(org.talend.core.model.components.filters.IComponentFactoryFilter) FileNotFoundException(java.io.FileNotFoundException) BusinessException(org.talend.commons.exception.BusinessException) FileFilter(java.io.FileFilter) Path(org.eclipse.core.runtime.Path) ComponentBundleToPath(org.talend.designer.core.model.components.ComponentBundleToPath) Bundle(org.osgi.framework.Bundle) ResourceBundle(java.util.ResourceBundle) IOException(java.io.IOException) IBrandingService(org.talend.core.ui.branding.IBrandingService) ComponentsCache(org.talend.core.model.component_cache.ComponentsCache) FileNotFoundException(java.io.FileNotFoundException) BusinessException(org.talend.commons.exception.BusinessException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ServiceReference(org.osgi.framework.ServiceReference) PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) EmfComponent(org.talend.designer.core.model.components.EmfComponent) ComponentInfo(org.talend.core.model.component_cache.ComponentInfo) File(java.io.File) BundleContext(org.osgi.framework.BundleContext)

Example 44 with Bundle

use of org.osgi.framework.Bundle in project KaiZen-OpenAPI-Editor by RepreZen.

the class Activator method start.

/*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework. BundleContext)
     */
public void start(BundleContext context) throws Exception {
    super.start(context);
    plugin = this;
    if (Display.getCurrent() != null && PlatformUI.isWorkbenchRunning()) {
        Bundle bundle = Platform.getBundle(PLUGIN_ID);
        addImage(bundle, Icons.assist_item.name(), "icons/assist_item_16.png");
        addImage(bundle, Icons.template_item.name(), "icons/template_item_16.png");
        // for quick outline, add icons from YEdit
        bundle = Platform.getBundle(org.dadacoalition.yedit.Activator.PLUGIN_ID);
        addImage(bundle, Icons.outline_document.name(), "icons/outline_document.gif");
        addImage(bundle, Icons.outline_mapping.name(), "icons/outline_mapping.gif");
        addImage(bundle, Icons.outline_scalar.name(), "icons/outline_scalar.gif");
        addImage(bundle, Icons.outline_mapping_scalar.name(), "icons/outline_mappingscalar.gif");
        addImage(bundle, Icons.outline_sequence.name(), "icons/outline_sequence.png");
    }
}
Also used : Bundle(org.osgi.framework.Bundle)

Example 45 with Bundle

use of org.osgi.framework.Bundle in project tdi-studio-se by Talend.

the class JetSkeletonManager method getSystemSkeletons.

/**
     * DOC xtan get the skeleton file: subprocess_header_java.skeleton.
     * 
     * @return
     */
private static List<String> getSystemSkeletons() {
    List<String> skeletonList = new ArrayList<String>();
    // here add the skeleton file in org.talend.designer.codegen\resources
    FileFilter skeletonFilter = new FileFilter() {

        @Override
        public boolean accept(final File file) {
            String fileName = file.getName();
            return file.isFile() && fileName.charAt(0) != '.' && (fileName.endsWith(SKELETON_SUFFIX) || fileName.endsWith(INCLUDEFILEINJET_SUFFIX));
        }
    };
    for (TemplateUtil template : CodeGeneratorInternalTemplatesFactoryProvider.getInstance().getTemplates()) {
        Bundle b = Platform.getBundle(template.getJetPluginRepository());
        URL resourcesUrl = null;
        try {
            resourcesUrl = FileLocator.toFileURL(FileLocator.find(b, new Path(template.getTemplateRelativeUri()), null));
        } catch (IOException e) {
            ExceptionHandler.process(e);
        }
        if (resourcesUrl != null) {
            File resourcesDir = new File(resourcesUrl.getFile());
            File[] skeletonFiles = resourcesDir.listFiles(skeletonFilter);
            if (skeletonFiles != null) {
                for (File file : skeletonFiles) {
                    // path
                    skeletonList.add(file.getAbsolutePath());
                }
            }
        }
    }
    return skeletonList;
}
Also used : Path(org.eclipse.core.runtime.Path) TemplateUtil(org.talend.designer.codegen.config.TemplateUtil) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileFilter(java.io.FileFilter) IFile(org.eclipse.core.resources.IFile) File(java.io.File) URL(java.net.URL)

Aggregations

Bundle (org.osgi.framework.Bundle)2527 Test (org.junit.Test)674 URL (java.net.URL)389 BundleContext (org.osgi.framework.BundleContext)319 ArrayList (java.util.ArrayList)299 File (java.io.File)298 IOException (java.io.IOException)281 BundleException (org.osgi.framework.BundleException)273 HashMap (java.util.HashMap)155 ServiceReference (org.osgi.framework.ServiceReference)152 Hashtable (java.util.Hashtable)131 Path (org.eclipse.core.runtime.Path)126 HashSet (java.util.HashSet)98 InputStream (java.io.InputStream)94 List (java.util.List)87 IStatus (org.eclipse.core.runtime.IStatus)86 Status (org.eclipse.core.runtime.Status)82 Map (java.util.Map)74 BundleWiring (org.osgi.framework.wiring.BundleWiring)74 Version (org.osgi.framework.Version)73