001package org.kuali.common.jute.project.maven; 002 003import static com.google.common.base.Functions.compose; 004import static org.kuali.common.jute.base.Exceptions.illegalState; 005import static org.kuali.common.jute.project.maven.ProjectFunctions.metadataPathFunction; 006import static org.kuali.common.jute.project.maven.ProjectFunctions.projectIdentifierFunction; 007import static org.kuali.common.jute.reflect.Reflection.checkNoNulls; 008 009import java.io.File; 010import java.io.IOException; 011 012import javax.inject.Inject; 013import javax.inject.Provider; 014 015import org.kuali.common.jute.json.JsonService; 016 017import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 018 019@JsonDeserialize(builder = WriteMetadataRunnable.Builder.class) 020public final class WriteMetadataRunnable implements Runnable { 021 022 private final ProjectMetadata metadata; 023 private final DirectoryContext dirs; 024 private final JsonService json; 025 026 @Override 027 public void run() { 028 Project project = metadata.getProject(); 029 File output = dirs.getMain().getOutput(); 030 String path = compose(metadataPathFunction(), projectIdentifierFunction()).apply(project); 031 File file = new File(output, path); 032 try { 033 json.write(file, metadata); 034 } catch (IOException e) { 035 throw illegalState(e); 036 } 037 } 038 039 private WriteMetadataRunnable(Builder builder) { 040 this.metadata = builder.metadata; 041 this.json = builder.json; 042 this.dirs = builder.dirs; 043 } 044 045 public static Builder builder() { 046 return new Builder(); 047 } 048 049 public static class Builder implements org.apache.commons.lang3.builder.Builder<WriteMetadataRunnable>, Provider<WriteMetadataRunnable> { 050 051 private DirectoryContext dirs; 052 private ProjectMetadata metadata; 053 private JsonService json; 054 055 @Inject 056 public Builder withDirectoryContext(DirectoryContext dirs) { 057 this.dirs = dirs; 058 return this; 059 } 060 061 @Inject 062 public Builder withMetadata(ProjectMetadata metadata) { 063 this.metadata = metadata; 064 return this; 065 } 066 067 @Inject 068 public Builder withJson(JsonService json) { 069 this.json = json; 070 return this; 071 } 072 073 @Override 074 public WriteMetadataRunnable get() { 075 return build(); 076 } 077 078 @Override 079 public WriteMetadataRunnable build() { 080 return checkNoNulls(new WriteMetadataRunnable(this)); 081 } 082 } 083 084 public JsonService getJson() { 085 return json; 086 } 087 088 public ProjectMetadata getMetadata() { 089 return metadata; 090 } 091 092 public DirectoryContext getDirs() { 093 return dirs; 094 } 095 096}