Search in sources :

Example 41 with FileObject

use of javax.tools.FileObject in project druid by druid-io.

the class OssDataSegmentPuller method buildFileObject.

private FileObject buildFileObject(final URI uri) throws OSSException {
    final CloudObjectLocation coords = new CloudObjectLocation(OssUtils.checkURI(uri));
    final OSSObjectSummary objectSummary = OssUtils.getSingleObjectSummary(client, coords.getBucket(), coords.getPath());
    final String path = uri.getPath();
    return new FileObject() {

        OSSObject ossObject = null;

        @Override
        public URI toUri() {
            return uri;
        }

        @Override
        public String getName() {
            final String ext = Files.getFileExtension(path);
            return Files.getNameWithoutExtension(path) + (Strings.isNullOrEmpty(ext) ? "" : ("." + ext));
        }

        /**
         * Returns an input stream for an OSS object. The returned input stream is not thread-safe.
         */
        @Override
        public InputStream openInputStream() throws IOException {
            try {
                if (ossObject == null) {
                    // lazily promote to full GET
                    ossObject = client.getObject(objectSummary.getBucketName(), objectSummary.getKey());
                }
                final InputStream in = ossObject.getObjectContent();
                final Closer closer = Closer.create();
                closer.register(in);
                closer.register(ossObject);
                return new FilterInputStream(in) {

                    @Override
                    public void close() throws IOException {
                        closer.close();
                    }
                };
            } catch (OSSException e) {
                throw new IOE(e, "Could not load OSS URI [%s]", uri);
            }
        }

        @Override
        public OutputStream openOutputStream() {
            throw new UOE("Cannot stream OSS output");
        }

        @Override
        public Reader openReader(boolean ignoreEncodingErrors) {
            throw new UOE("Cannot open reader");
        }

        @Override
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            throw new UOE("Cannot open character sequence");
        }

        @Override
        public Writer openWriter() {
            throw new UOE("Cannot open writer");
        }

        @Override
        public long getLastModified() {
            return objectSummary.getLastModified().getTime();
        }

        @Override
        public boolean delete() {
            throw new UOE("Cannot delete OSS items anonymously. jetS3t doesn't support authenticated deletes easily.");
        }
    };
}
Also used : Closer(org.apache.druid.java.util.common.io.Closer) OSSObjectSummary(com.aliyun.oss.model.OSSObjectSummary) FilterInputStream(java.io.FilterInputStream) OSSObject(com.aliyun.oss.model.OSSObject) CloudObjectLocation(org.apache.druid.data.input.impl.CloudObjectLocation) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) OSSException(com.aliyun.oss.OSSException) UOE(org.apache.druid.java.util.common.UOE) FileObject(javax.tools.FileObject) IOE(org.apache.druid.java.util.common.IOE)

Example 42 with FileObject

use of javax.tools.FileObject in project dubbo by alibaba.

the class TypeUtils method getResource.

static URL getResource(ProcessingEnvironment processingEnv, CharSequence type) {
    String relativeName = getResourceName(type);
    URL resource = null;
    try {
        if (relativeName != null) {
            FileObject fileObject = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", relativeName);
            resource = fileObject.toUri().toURL();
            // try to open it
            resource.getContent();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return resource;
}
Also used : FileObject(javax.tools.FileObject) IOException(java.io.IOException) URL(java.net.URL)

Example 43 with FileObject

use of javax.tools.FileObject in project androidannotations by androidannotations.

the class FileHelper method findRootProjectHolder.

/**
 * We use a dirty trick to find the AndroidManifest.xml file, since it's not
 * available in the classpath. The idea is quite simple : create a fake class
 * file, retrieve its URI, and start going up in parent folders to find the
 * AndroidManifest.xml file. Any better solution will be appreciated.
 */
public static FileHolder findRootProjectHolder(ProcessingEnvironment processingEnv) throws FileNotFoundException {
    FileHolder rootProjectHolder = findKaptRootProjectHolder(processingEnv);
    if (rootProjectHolder != null) {
        return rootProjectHolder;
    }
    Filer filer = processingEnv.getFiler();
    FileObject dummySourceFile;
    try {
        dummySourceFile = filer.createResource(StandardLocation.SOURCE_OUTPUT, "", "dummy" + System.currentTimeMillis());
    } catch (IOException ignored) {
        throw new FileNotFoundException();
    }
    return createFileHolder(dummySourceFile.toUri().toString());
}
Also used : FileNotFoundException(java.io.FileNotFoundException) FileObject(javax.tools.FileObject) IOException(java.io.IOException) Filer(javax.annotation.processing.Filer)

Example 44 with FileObject

use of javax.tools.FileObject in project Payara by payara.

the class RestModelExtensionProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> elements, RoundEnvironment env) {
    Messager messager = processingEnv.getMessager();
    BufferedWriter bw = null;
    try {
        Map<String, List<String>> classes = new HashMap<String, List<String>>();
        for (TypeElement te : elements) {
            for (Element e : env.getElementsAnnotatedWith(te)) {
                final RestModelExtension annotation = e.getAnnotation(RestModelExtension.class);
                final String parent = annotation.parent();
                List<String> list = classes.get(parent);
                if (list == null) {
                    list = new ArrayList<String>();
                    classes.put(parent, list);
                }
                list.add(e.toString());
            }
        }
        if (!classes.isEmpty()) {
            final Filer filer = processingEnv.getFiler();
            FileObject fo = filer.createResource(StandardLocation.CLASS_OUTPUT, "", "META-INF/restmodelextensions");
            bw = new BufferedWriter(fo.openWriter());
            // parent model:model extension
            for (Map.Entry<String, List<String>> entry : classes.entrySet()) {
                final String key = entry.getKey();
                for (String ext : entry.getValue()) {
                    bw.write(key + ":" + ext + "\n");
                }
            }
            bw.close();
        }
    } catch (IOException ex) {
        messager.printMessage(Kind.ERROR, ex.getLocalizedMessage());
        if (bw != null) {
            try {
                bw.close();
            } catch (Exception e) {
            }
        }
    }
    return true;
}
Also used : Messager(javax.annotation.processing.Messager) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) RestModelExtension(org.glassfish.admin.rest.composite.RestModelExtension) IOException(java.io.IOException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) ArrayList(java.util.ArrayList) List(java.util.List) FileObject(javax.tools.FileObject) Filer(javax.annotation.processing.Filer) HashMap(java.util.HashMap) Map(java.util.Map)

Example 45 with FileObject

use of javax.tools.FileObject in project gradle by gradle.

the class IncrementalFiler method createResource.

@Override
public final FileObject createResource(JavaFileManager.Location location, CharSequence pkg, CharSequence relativeName, Element... originatingElements) throws IOException {
    // Prefer having javac validate the location over us, by calling it first.
    FileObject resource = delegate.createResource(location, pkg, relativeName, originatingElements);
    strategy.recordGeneratedResource(location, pkg, relativeName, originatingElements);
    return resource;
}
Also used : FileObject(javax.tools.FileObject) JavaFileObject(javax.tools.JavaFileObject)

Aggregations

FileObject (javax.tools.FileObject)91 IOException (java.io.IOException)57 TypeElement (javax.lang.model.element.TypeElement)19 File (java.io.File)18 PrintWriter (java.io.PrintWriter)16 Element (javax.lang.model.element.Element)14 Writer (java.io.Writer)13 Filer (javax.annotation.processing.Filer)13 BufferedWriter (java.io.BufferedWriter)12 ArrayList (java.util.ArrayList)12 OutputStream (java.io.OutputStream)11 JavaFileObject (javax.tools.JavaFileObject)11 OutputStreamWriter (java.io.OutputStreamWriter)10 Properties (java.util.Properties)10 InputStream (java.io.InputStream)8 URI (java.net.URI)8 FilerException (javax.annotation.processing.FilerException)7 MainInfo (com.predic8.membrane.annot.model.MainInfo)6 BufferedReader (java.io.BufferedReader)6 FileWriter (java.io.FileWriter)6