001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.devtools.restart.classloader;
018
019import java.io.Serializable;
020
021import org.springframework.util.Assert;
022
023/**
024 * A single file that may be served from a {@link ClassLoader}. Can be used to represent
025 * files that have been added, modified or deleted since the original JAR was created.
026 *
027 * @author Phillip Webb
028 * @since 1.3.0
029 * @see ClassLoaderFileRepository
030 */
031public class ClassLoaderFile implements Serializable {
032
033        private static final long serialVersionUID = 1;
034
035        private final Kind kind;
036
037        private final byte[] contents;
038
039        private final long lastModified;
040
041        /**
042         * Create a new {@link ClassLoaderFile} instance.
043         * @param kind the kind of file
044         * @param contents the file contents
045         */
046        public ClassLoaderFile(Kind kind, byte[] contents) {
047                this(kind, System.currentTimeMillis(), contents);
048        }
049
050        /**
051         * Create a new {@link ClassLoaderFile} instance.
052         * @param kind the kind of file
053         * @param lastModified the last modified time
054         * @param contents the file contents
055         */
056        public ClassLoaderFile(Kind kind, long lastModified, byte[] contents) {
057                Assert.notNull(kind, "Kind must not be null");
058                Assert.isTrue((kind != Kind.DELETED) ? contents != null : contents == null,
059                                () -> "Contents must " + ((kind != Kind.DELETED) ? "not " : "")
060                                                + "be null");
061                this.kind = kind;
062                this.lastModified = lastModified;
063                this.contents = contents;
064        }
065
066        /**
067         * Return the file {@link Kind} (added, modified, deleted).
068         * @return the kind
069         */
070        public Kind getKind() {
071                return this.kind;
072        }
073
074        /**
075         * Return the time that the file was last modified.
076         * @return the last modified time
077         */
078        public long getLastModified() {
079                return this.lastModified;
080        }
081
082        /**
083         * Return the contents of the file as a byte array or {@code null} if
084         * {@link #getKind()} is {@link Kind#DELETED}.
085         * @return the contents or {@code null}
086         */
087        public byte[] getContents() {
088                return this.contents;
089        }
090
091        /**
092         * The kinds of class load files.
093         */
094        public enum Kind {
095
096                /**
097                 * The file has been added since the original JAR was created.
098                 */
099                ADDED,
100
101                /**
102                 * The file has been modified since the original JAR was created.
103                 */
104                MODIFIED,
105
106                /**
107                 * The file has been deleted since the original JAR was created.
108                 */
109                DELETED
110
111        }
112
113}