Search in sources :

Example 1 with CacheStoringEmfSerializerDataSource

use of org.bimserver.plugins.serializers.CacheStoringEmfSerializerDataSource in project BIMserver by opensourceBIM.

the class JsonConverter method toJson.

public void toJson(Object object, JsonWriter out) throws IOException, SerializerException {
    if (object instanceof SBase) {
        SBase base = (SBase) object;
        out.beginObject();
        out.name("__type");
        out.value(base.getSClass().getSimpleName());
        for (SField field : base.getSClass().getAllFields()) {
            out.name(field.getName());
            toJson(base.sGet(field), out);
        }
        out.endObject();
    } else if (object instanceof Collection) {
        Collection<?> collection = (Collection<?>) object;
        out.beginArray();
        for (Object value : collection) {
            toJson(value, out);
        }
        out.endArray();
    } else if (object instanceof Date) {
        out.value(((Date) object).getTime());
    } else if (object instanceof DataHandler) {
        DataHandler dataHandler = (DataHandler) object;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        if (dataHandler.getDataSource() instanceof CacheStoringEmfSerializerDataSource) {
            CacheStoringEmfSerializerDataSource cacheStoringEmfSerializerDataSource = (CacheStoringEmfSerializerDataSource) dataHandler.getDataSource();
            cacheStoringEmfSerializerDataSource.writeToOutputStream(baos, null);
            out.value(new String(Base64.encodeBase64(baos.toByteArray()), Charsets.UTF_8));
        } else {
            InputStream inputStream = dataHandler.getInputStream();
            IOUtils.copy(inputStream, baos);
            out.value(new String(Base64.encodeBase64(baos.toByteArray()), Charsets.UTF_8));
        }
    } else if (object instanceof byte[]) {
        byte[] data = (byte[]) object;
        out.value(new String(Base64.encodeBase64(data), Charsets.UTF_8));
    } else if (object instanceof String) {
        out.value((String) object);
    } else if (object instanceof Number) {
        out.value((Number) object);
    } else if (object instanceof Enum) {
        out.value(object.toString());
    } else if (object instanceof Boolean) {
        out.value((Boolean) object);
    } else if (object == null) {
        out.nullValue();
    } else {
        throw new UnsupportedOperationException(object.toString());
    }
}
Also used : CacheStoringEmfSerializerDataSource(org.bimserver.plugins.serializers.CacheStoringEmfSerializerDataSource) InputStream(java.io.InputStream) SField(org.bimserver.shared.meta.SField) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) SBase(org.bimserver.shared.meta.SBase) Collection(java.util.Collection) JsonObject(com.google.gson.JsonObject)

Example 2 with CacheStoringEmfSerializerDataSource

use of org.bimserver.plugins.serializers.CacheStoringEmfSerializerDataSource in project BIMserver by opensourceBIM.

the class LongDownloadOrCheckoutAction method convertModelToCheckoutResult.

protected SCheckoutResult convertModelToCheckoutResult(Project project, String username, IfcModelInterface model, RenderEnginePlugin renderEnginePlugin, DownloadParameters downloadParameters) throws UserException, NoSerializerFoundException {
    SCheckoutResult checkoutResult = new SCheckoutResult();
    checkoutResult.setSerializerOid(downloadParameters.getSerializerOid());
    if (model.isValid()) {
        checkoutResult.setProjectName(project.getName());
        checkoutResult.setRevisionNr(model.getModelMetaData().getRevisionId());
        try {
            Serializer serializer = getBimServer().getSerializerFactory().create(project, username, model, renderEnginePlugin, downloadParameters);
            if (serializer == null) {
                throw new UserException("Error, no serializer found " + downloadParameters.getSerializerOid());
            }
            if (getBimServer().getServerSettingsCache().getServerSettings().getCacheOutputFiles() && serializer.allowCaching()) {
                if (getBimServer().getDiskCacheManager().contains(downloadParameters)) {
                    checkoutResult.setFile(new CachingDataHandler(getBimServer().getDiskCacheManager(), downloadParameters));
                } else {
                    checkoutResult.setFile(new DataHandler(new CacheStoringEmfSerializerDataSource(serializer, model.getModelMetaData().getName(), getBimServer().getDiskCacheManager().startCaching(downloadParameters))));
                }
            } else {
                checkoutResult.setFile(new DataHandler(new EmfSerializerDataSource(serializer, model.getModelMetaData().getName())));
            }
        } catch (SerializerException e) {
            LOGGER.error("", e);
        }
    }
    return checkoutResult;
}
Also used : CacheStoringEmfSerializerDataSource(org.bimserver.plugins.serializers.CacheStoringEmfSerializerDataSource) SCheckoutResult(org.bimserver.interfaces.objects.SCheckoutResult) UserException(org.bimserver.shared.exceptions.UserException) DataHandler(javax.activation.DataHandler) SerializerException(org.bimserver.plugins.serializers.SerializerException) CacheStoringEmfSerializerDataSource(org.bimserver.plugins.serializers.CacheStoringEmfSerializerDataSource) EmfSerializerDataSource(org.bimserver.plugins.serializers.EmfSerializerDataSource) MessagingSerializer(org.bimserver.plugins.serializers.MessagingSerializer) Serializer(org.bimserver.plugins.serializers.Serializer)

Example 3 with CacheStoringEmfSerializerDataSource

use of org.bimserver.plugins.serializers.CacheStoringEmfSerializerDataSource in project BIMserver by opensourceBIM.

the class TestSimultaniousDownloadWithCaching method start.

private void start() {
    BimServerConfig config = new BimServerConfig();
    Path homeDir = Paths.get("home");
    try {
        if (Files.isDirectory(homeDir)) {
            PathUtils.removeDirectoryWithContent(homeDir);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    config.setClassPath(System.getProperty("java.class.path"));
    config.setHomeDir(homeDir);
    config.setPort(8080);
    config.setStartEmbeddedWebServer(true);
    config.setResourceFetcher(new LocalDevelopmentResourceFetcher(Paths.get("../")));
    final BimServer bimServer = new BimServer(config);
    try {
        LocalDevPluginLoader.loadPlugins(bimServer.getPluginBundleManager(), null);
        bimServer.start();
        if (bimServer.getServerInfo().getServerState() == ServerState.NOT_SETUP) {
            bimServer.getService(AdminInterface.class).setup("http://localhost", "Administrator", "admin@bimserver.org", "admin", null, null, null);
        }
    } catch (PluginException e2) {
        e2.printStackTrace();
    } catch (ServerException e) {
        e.printStackTrace();
    } catch (DatabaseInitException e) {
        e.printStackTrace();
    } catch (BimserverDatabaseException e) {
        e.printStackTrace();
    } catch (DatabaseRestartRequiredException e) {
        e.printStackTrace();
    } catch (UserException e) {
        e.printStackTrace();
    }
    try {
        final ServiceMap serviceMap = bimServer.getServiceFactory().get(AccessMethod.INTERNAL);
        ServiceInterface serviceInterface = serviceMap.get(ServiceInterface.class);
        SettingsInterface settingsInterface = serviceMap.get(SettingsInterface.class);
        final AuthInterface authInterface = serviceMap.get(AuthInterface.class);
        serviceInterface = bimServer.getServiceFactory().get(authInterface.login("admin@bimserver.org", "admin"), AccessMethod.INTERNAL).get(ServiceInterface.class);
        settingsInterface.setCacheOutputFiles(true);
        settingsInterface.setGenerateGeometryOnCheckin(false);
        final SProject project = serviceMap.getServiceInterface().addProject("test", "ifc2x3tc1");
        SDeserializerPluginConfiguration deserializerByName = serviceMap.getServiceInterface().getDeserializerByName("IfcStepDeserializer");
        Path file = Paths.get("../TestData/data/AC11-Institute-Var-2-IFC.ifc");
        serviceInterface.checkinSync(project.getOid(), "test", deserializerByName.getOid(), file.toFile().length(), file.getFileName().toString(), new DataHandler(new FileDataSource(file.toFile())), false);
        final SProject projectUpdate = serviceMap.getServiceInterface().getProjectByPoid(project.getOid());
        ThreadPoolExecutor executor = new ThreadPoolExecutor(20, 20, 1, TimeUnit.HOURS, new ArrayBlockingQueue<Runnable>(1000));
        for (int i = 0; i < 20; i++) {
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    try {
                        ServiceMap serviceMap2 = bimServer.getServiceFactory().get(authInterface.login("admin@bimserver.org", "admin"), AccessMethod.INTERNAL);
                        SSerializerPluginConfiguration serializerPluginConfiguration = serviceMap.getServiceInterface().getSerializerByName("Ifc2x3");
                        Long download = serviceMap2.getServiceInterface().download(Collections.singleton(projectUpdate.getLastRevisionId()), DefaultQueries.allAsString(), serializerPluginConfiguration.getOid(), true);
                        SDownloadResult downloadData = serviceMap2.getServiceInterface().getDownloadData(download);
                        if (downloadData.getFile().getDataSource() instanceof CacheStoringEmfSerializerDataSource) {
                            CacheStoringEmfSerializerDataSource c = (CacheStoringEmfSerializerDataSource) downloadData.getFile().getDataSource();
                            try {
                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                c.writeToOutputStream(baos, null);
                                System.out.println(baos.size());
                            } catch (SerializerException e) {
                                e.printStackTrace();
                            }
                        } else {
                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            IOUtils.copy(downloadData.getFile().getInputStream(), baos);
                            System.out.println(baos.size());
                        }
                        serviceMap2.getServiceInterface().cleanupLongAction(download);
                    } catch (ServerException e) {
                        e.printStackTrace();
                    } catch (UserException e) {
                        e.printStackTrace();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (PublicInterfaceNotFoundException e1) {
                        e1.printStackTrace();
                    }
                }
            });
        }
        executor.shutdown();
        executor.awaitTermination(1, TimeUnit.HOURS);
        bimServer.stop();
    } catch (ServerException e1) {
        e1.printStackTrace();
    } catch (UserException e1) {
        e1.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (PublicInterfaceNotFoundException e2) {
        e2.printStackTrace();
    }
}
Also used : AuthInterface(org.bimserver.shared.interfaces.AuthInterface) SDeserializerPluginConfiguration(org.bimserver.interfaces.objects.SDeserializerPluginConfiguration) ServiceMap(org.bimserver.webservices.ServiceMap) CacheStoringEmfSerializerDataSource(org.bimserver.plugins.serializers.CacheStoringEmfSerializerDataSource) FileNotFoundException(java.io.FileNotFoundException) DataHandler(javax.activation.DataHandler) BimServerConfig(org.bimserver.BimServerConfig) SProject(org.bimserver.interfaces.objects.SProject) LocalDevelopmentResourceFetcher(org.bimserver.shared.LocalDevelopmentResourceFetcher) DatabaseInitException(org.bimserver.database.berkeley.DatabaseInitException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) ServiceInterface(org.bimserver.shared.interfaces.ServiceInterface) FileDataSource(javax.activation.FileDataSource) UserException(org.bimserver.shared.exceptions.UserException) Path(java.nio.file.Path) ServerException(org.bimserver.shared.exceptions.ServerException) BimServer(org.bimserver.BimServer) PluginException(org.bimserver.shared.exceptions.PluginException) SDownloadResult(org.bimserver.interfaces.objects.SDownloadResult) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SerializerException(org.bimserver.plugins.serializers.SerializerException) AdminInterface(org.bimserver.shared.interfaces.AdminInterface) SettingsInterface(org.bimserver.shared.interfaces.SettingsInterface) PublicInterfaceNotFoundException(org.bimserver.shared.exceptions.PublicInterfaceNotFoundException) DatabaseRestartRequiredException(org.bimserver.database.DatabaseRestartRequiredException) SSerializerPluginConfiguration(org.bimserver.interfaces.objects.SSerializerPluginConfiguration) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Aggregations

DataHandler (javax.activation.DataHandler)3 CacheStoringEmfSerializerDataSource (org.bimserver.plugins.serializers.CacheStoringEmfSerializerDataSource)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 SerializerException (org.bimserver.plugins.serializers.SerializerException)2 UserException (org.bimserver.shared.exceptions.UserException)2 JsonObject (com.google.gson.JsonObject)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 Collection (java.util.Collection)1 Date (java.util.Date)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 FileDataSource (javax.activation.FileDataSource)1 BimServer (org.bimserver.BimServer)1 BimServerConfig (org.bimserver.BimServerConfig)1 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)1 DatabaseRestartRequiredException (org.bimserver.database.DatabaseRestartRequiredException)1 DatabaseInitException (org.bimserver.database.berkeley.DatabaseInitException)1 SCheckoutResult (org.bimserver.interfaces.objects.SCheckoutResult)1